我正在使用UIAlertview
并在viewdidload()
中的一个活动指示符中。但是我尝试在延迟后将其从超级视图中删除但是在使用以下代码删除UIAlertview
之后我不是能够在应用程序中执行任何操作。就像新的透明图层仍然在我的视图上方运行。
CODE
-(void)startAlertActivity
{
_alertView = [[UIAlertView alloc] initWithTitle:@"Loading "
message:@"\n"
delegate:self
cancelButtonTitle:nil
otherButtonTitles:nil];
spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
spinner.center = CGPointMake(139.5, 75.5); // .5 so it doesn't blur
[_alertView addSubview:spinner];
[spinner startAnimating];
[_alertView show];
[_alertView performSelector:@selector(stopAlertActivity) withObject:self afterDelay:5.0];
}
-(void)stopAlertActivity
{ [spinner stopAnimating];
[_alertView dismissWithClickedButtonIndex:0 animated:YES];
}
ITS看起来像一个仍然在屏幕上运行的透明层,我该如何关闭它?
示例图片....
对我来说,警报现在不在屏幕上,但背景是浅蓝色
CRASH REPORT
[UIAlertView stopAlertActivity]: unrecognized selector sent to instance 0x9ab5eb0
2013-08-02 12:20:43.822 AssamKart[5719:12203] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIAlertView stopAlertActivity]: unrecognized selector sent to instance 0x9ab5eb0'
*** First throw call stack:
答案 0 :(得分:1)
您需要更改此行:
[_alertView performSelector:@selector(stopAlertActivity) withObject:self afterDelay:5.0];
为:
[self performSelector:@selector(stopAlertActivity) withObject:nil afterDelay:5.0];
stopAlertActivity
方法是self
的方法,而不是警报视图。并且您无法将对象传递给选择器,因为stopAlertActivity
不接受任何参数。
答案 1 :(得分:1)
你应该真的使用dispatch_after
_alertView = [[UIAlertView alloc] initWithTitle:@"Loading "
message:@"\n"
delegate:self
cancelButtonTitle:nil
otherButtonTitles:nil];
spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
spinner.center = CGPointMake(139.5, 75.5); // .5 so it doesn't blur
[_alertView addSubview:spinner];
[spinner startAnimating];
[_alertView show];
double delayInSeconds = 5.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
[spinner stopAnimating];
[_alertView dismissWithClickedButtonIndex:0 animated:YES];
});
答案 2 :(得分:0)
您无法通过将其从superView中删除来解除alertView
。相反,你必须致电dismissWithClickedButtonIndex:animated:
。这是正确解除它的唯一方法,请参阅docs
在您的情况下,我将定义一个方法,alertView
作为执行dismissWithClickedButtonIndex:animated:
的参数,并在延迟后执行此方法。
答案 3 :(得分:0)
您应该关闭警报视图,而不是从superview中删除,因为它附加到半透明背景。延迟后致电stopAlertActivity
。