uialertview / 스레딩 문제
-
19-09-2019 - |
문제
내 앱에서 특정 버튼을 누르면 메소드를 호출합니다.postButtonClicked:
) 별도의 스레드에서 웹 서비스를 구문 분석합니다. 그런 다음 사용자 AN을 표시합니다 UIAlertView
전화가 성공했는지 여부를 알리기 위해.
다음은 내가 사용하는 코드입니다.
- (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:
, 앱은 정상적으로 작동합니다.
이것을 고치기 위해 무엇을 해야하는지 잘 모르겠습니다.
해결책
Alert View Delegate 메소드의 내용이 성공/실패 메시지와 관련이 있는지 여부에 관계없이 여전히 문제가 될 수 있으므로 해당 메소드의 내용을 보여주는 것이 답을 얻는 유일한 방법 일 수 있습니다. 어느 쪽이든, Alert View Delegate 메소드는 성공/실패 메시지에서 확인을 누를 때 호출됩니다. self
. 성공/실패 경고를 해산 할 때 메소드가 호출되는 방법을 원하지 않는 경우 대의원을 설정하지 마십시오.
제휴하지 않습니다 StackOverflow