0

我在UIAlertview里面使用了一个活动指示器。viewdidload() 但是我尝试在延迟后将其从超级视图中删除,但是在UIAlertview使用以下代码删除后,我无法在应用程序中执行任何操作。就像一个新的透明层仍在我的上方运行看法。

代码

 -(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];
    }

它看起来像一个仍在屏幕上运行的透明层,我该如何关闭它?

示例图像.... 在此处输入图像描述

对我来说,警报现在不在屏幕上,但背景是浅蓝色的

崩溃报告

[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:
4

4 回答 4

1

您需要更改此行:

[_alertView performSelector:@selector(stopAlertActivity) withObject:self afterDelay:5.0];

至:

[self performSelector:@selector(stopAlertActivity) withObject:nil afterDelay:5.0];

stopAlertActivity方法是 的方法self,而不是警报视图。而且您不能将对象传递给选择器,因为stopAlertActivity它不带任何参数。

于 2013-08-02T07:01:30.820 回答
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];
});
于 2013-08-02T07:04:38.537 回答
0

您应该关闭警报视图,而不是从超级视图中删除,因为它附加到半透明背景。stopAlertActivity延迟后打电话。

于 2013-08-02T06:43:14.830 回答
0

您不能通过alertView将其从 superView 中删除来解除它。相反,您必须调用dismissWithClickedButtonIndex:animated:. 这是正确解除它的唯一方法,请参阅文档
在您的情况下,我将使用alertViewas 参数定义一个方法 execute dismissWithClickedButtonIndex:animated:,并在延迟后执行此方法。

于 2013-08-02T06:42:11.460 回答