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];
}
});
});
}
왜 이렇게해야합니까? 경고보기가 사라진 후 잠시 동안 대기 화면을 표시합니다. – xhan
내가 ok 버튼을 클릭 할 때 2 분이 걸리는 프로세스를 업데이트해야하는데, 왜 이것이 필요한지. – MaheshBabu