1

在我的应用程序中,当按下某个按钮时,我会调用一个方法 ( postButtonClicked:),该方法在单独的线程上解析 Web 服务。然后我向用户显示 anUIAlertView以通知他们呼叫是否成功。

这是我使用的代码:

- (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 {

    ...

}

这一切都很好,直到我将它添加alertView:clickedButtonAtIndex:到视图控制器(另一个UIAlertView我显示需要)。现在,每次我打电话postButtonClicked:时,应用程序都会崩溃。但是,如果我删除alertView:clickedButtonAtIndex:,然后调用postButtonClicked:,该应用程序可以正常工作。

我不确定我需要做什么来解决这个问题。

4

1 回答 1

0

无论警报视图委托方法的内容是否与成功/失败消息相关,它仍然可能是一个问题,因此向我们展示该方法的内容可能是让您得到答案的唯一方法。无论哪种方式,当您在成功/失败消息上点击 OK 时,都会调用警报视图委托方法,因为您将警报委托设置为self. 如果您不希望在关闭成功/失败警报时调用该方法,请不要设置委托。

于 2010-02-09T15:15:30.893 回答