2011-01-13 7 views
2

두 개의 단추 이름이 ok 인 단순 경보보기를 사용하고 있습니다. 취소하십시오.해당 경고보기 단추를 클릭하면 경고보기에 활동 표시기를 배치하는 방법

언제든지 확인을 누르면 알림보기가 취소됩니다.

하지만 알림보기 버튼을 클릭 할 때 알림 버튼을 클릭 할 필요가 있습니다. 은 같은 경고보기에서 2 분 동 안 실행되고 종료됩니다. 취소를 클릭하면 정상적으로 종료됩니다.

누구든 도와주세요.

미리 감사드립니다.

+0

왜 이렇게해야합니까? 경고보기가 사라진 후 잠시 동안 대기 화면을 표시합니다. – xhan

+0

내가 ok 버튼을 클릭 할 때 2 분이 걸리는 프로세스를 업데이트해야하는데, 왜 이것이 필요한지. – MaheshBabu

답변

3

OK 단추의 UI 컨트롤 이벤트를 수정하여 해당 이벤트 처리기가 해당 단추에 대해 호출되고 장기 실행 작업이 끝날 때까지 경고보기가 닫히지 않도록 할 수 있습니다. 해당 이벤트 핸들러에서 뷰에 활동 표시기를 연결하고 GCD를 사용하여 타스크를 비동기 적으로 시작하십시오.

#import <dispatch/dispatch.h> 

// ... 

UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"Test" message:@"Message" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK" ,nil]; 

for (UIView *subview in alert.subviews) 
{ 
    if ([subview isKindOfClass:[UIControl class]] && subview.tag == 2) { 
     UIControl* button = (UIControl*) subview; 
     [button addTarget:self action:@selector(buttonOKPressed:) forControlEvents:UIControlEventTouchUpInside]; 
     [button removeTarget:alert action:nil forControlEvents:UIControlEventAllEvents]; 
    } 
} 
[alert show]; 
[alert release]; 

// ... 

-(void) buttonOKPressed:(UIControl*) sender { 

    [sender removeTarget:self action:nil forControlEvents:UIControlEventAllEvents]; 
    UIAlertView* alert = (UIAlertView*)[sender superview]; 
    CGRect alertFrame = alert.frame; 
    UIActivityIndicatorView* activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge]; 
    activityIndicator.frame = CGRectMake(0,alertFrame.size.height, alertFrame.size.width,30); 
    activityIndicator.hidden = NO; 
    activityIndicator.alpha = 0.0; 
    activityIndicator.contentMode = UIViewContentModeCenter; 
    [activityIndicator startAnimating]; 
    [alert addSubview:activityIndicator]; 
    [activityIndicator release]; 
    [UIView animateWithDuration:0.3 animations:^{ 
     alert.frame = CGRectMake(alertFrame.origin.x, alertFrame.origin.y, alertFrame.size.width, alertFrame.size.height+50); 
     activityIndicator.alpha = 1.0; 

    }];   
    //alert.userInteractionEnabled = NO; // uncomment this, if you want to disable all buttons (cancel button) 
    dispatch_async(dispatch_get_global_queue(0,0), ^{ 
     [NSThread sleepForTimeInterval:5]; // replace this with your long-running task 
     dispatch_async(dispatch_get_main_queue(), ^{ 
      if (alert && alert.visible) { 
       [alert dismissWithClickedButtonIndex:alert.firstOtherButtonIndex animated:YES]; 
      } 
     }); 
    }); 
} 
관련 문제