AlertView ButtonIndex不起作用

时间:2012-08-14 00:13:08

标签: ios xcode uialertview alertview

忽略TextField因为buttonIndex不起作用所以我将textfield语法保留了。

-(IBAction)bm1:(id)sender {
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Set Bookmark" message:@"Type URL Here" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;

                      [alert show];
                      [alert release];
                      }
- (void)alertView:(UIAlertView *)alertview clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex == 0) {
    NSLog(@"It works");
}
}

2 个答案:

答案 0 :(得分:1)

您是否已将<UIAlertViewDelegate>附加到头文件?

@implementation MyClass : <UIAlertViewDelegate>

答案 1 :(得分:-1)

尝试使用uialertview.cancel按钮索引或alertview.firstOtherButtonIndex而不是0.这比使用幻数更具可读性。