UIAlertview not closing properly?

2019-09-05 02:46发布

问题:

I am using a UIAlertview and inside that one activity indicator in viewdidload() .But i tried to remove its from superview after a delay but after removing UIAlertview using following code i am not able to do anything in app.Is like a new transparent layer is still running above my view.

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 look like a transparent layer still running on screen,How can i close that?

example image....

for me that alert is not in screen now but the background is there in light blue color

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:

回答1:

you should really be using 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:

You need to change this line:

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

to:

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

The stopAlertActivity method is a method of self, not the alert view. And you can't pass an object to the selector because stopAlertActivity does not take any parameters.



回答3:

You cannot dismiss the alertView by removing it from its superView. Instead you have to call dismissWithClickedButtonIndex:animated:. This is the only way to dismiss it correctly, see the docs.
In your case, I would define a method with the alertView as argument that executes dismissWithClickedButtonIndex:animated:, and perform this method after the delay.



回答4:

You should dismiss the alert view, not remove from superview, because it is attached to a semi transparent background. Call stopAlertActivity after the delay.