开发者

UIAlertView / Threading issue

In my App, when a certain button is pressed, I call a method (postButtonClicked:) that parses a web service on a separate thread. I then display the user an UIAlertView to inform them whether the call was successful or not.

Here is the code I use:

- (void)postButtonClicked:(id)sender {
    [NSThread detachNewThreadSelector:@selector(postViaWebservices) toTarget:self withObject:nil];
}

- (void)postViaWebservices {

    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    WebServiceManager *wsm = [[WebServiceManager alloc] init];

    BOOL success = [wsm callPost];

    if (success) {
        [self performSelectorOnMainThread:@selector(postSuccess) withObject:nil waitUntilDone:NO];
    } else {
        [self performSelectorOnMainThread:@selector(postFailure) withObject:nil waitUntilDone:NO];
    }   

    [wsm release];

    [pool release];

}

- (void)postSuccess {

    UIAlertView *alert = [[UIAlertView alloc] 
                          initWithTitle:nil
                          message:@"Success message"
                          delegate:self
                          cancelButtonTitle:@"OK"
                          otherButtonTitles:nil];

    [alert show];
    [alert release];

}

- (void)postFailure {

    UIAlertView *alert = [[UIAlertView alloc] 
                          initWithTitle:nil
                          message:@"Failure message"
                          delegate:self
                          cancelButtonTitle:@"OK"
                          otherButtonTitles:nil];

    [alert show];
    [alert release];

}

- (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {

    ...

}

This all worked fine UNTIL I added the alertView:clickedButtonAtIndex: to the view controller (required for another UIAlertView开发者_运维问答 I display). Now, every time I call postButtonClicked:, the App crashes. However, if I remove alertView:clickedButtonAtIndex:, then call postButtonClicked:, the App works OK.

I'm not sure what I need to do to fix this.


Regardless of whether or not the content of the alert view delegate method is relevant to the success/fail messages, it could still be a problem, so showing us the contents of that method may be the only way to get you an answer. Either way, the alert view delegate method is called when you tap OK on the success/fail messages, because you're setting the alerts delegate as self. If you don't want the method to invoke when dismissing the success/fail alerts, don't set the delegate.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜