대리 방법"clickedButtonAtIndex:"은 불
-
22-09-2019 - |
문제
을 만들었 경고 보기는 두 개의 버튼으로 다음 코드를 사용하면 사용:
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle: title
message: msg delegate:nil cancelButtonTitle:@"Replay" otherButtonTitles:@"Highscore", nil];
[alertView show];
내가 코드를 실행하려는 경우 하나의 버튼 클릭합니다.그렇게하기 위해서는,추가했 다음과 같은 방법을 다할 수 있습니다.m file:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex==0) //Run some code
else //Other code
}
하지만 이 메소드는 호출하지 않는 경우 하나의 버튼!할 수 있는 누군가가 나에게 왜?
사전에 감사합니다,
Sagiftw
해결책
delegate:nil
how will the alert view associate a delegate if you specified there will be no delegates? Replace that part with
delegate:self
instead.
다른 팁
도 설정 대리인이 자신의 전무합니다.
I was calling UIAlertView's dismissWithClickedButtonIndex:animated: method from UITextField delegate method because I wanted to handle the keyboard return key as well:
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
if (textField.tag == 1001) {
[textField resignFirstResponder];
[_alert dismissWithClickedButtonIndex:1 animated:YES];
}
return YES;
}
This way method alertView:clickedButtonAtIndex: never gets called even if you set up proper delegate. Instead alertView:didDismissWithButtonIndex: does get called. So implement this method instead:
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
// your code here
}
If you just want to handle alert view's buttons only, those first call clickedButtonAtIndex and then didDismissWithButtonIndex.
in .h put UIActionSheetDelegate and in .m while creating a alertView put the delegate to self not nil as being done in the above case
The correct answer for this question is delegate:nil
. But in case that the delegate is already set to self, and the clickedButtonAtIndex
is still not working, try checking if you are popping another view controller ([self.navigationController popViewControllerAnimated:YES];
) after showing the alertview. This can also result to clickedButtonAtIndex
not being called. This is what happened to me.
Hope this helps someone.