UIAlertView输入提示

Eri*_*rik 7 objective-c uialertview ios

我在我的应用程序中需要输入提示,我已经尝试过了

// Create a new item
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"New item" message:@"Enter a name for the item" delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:@"Add", nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
[alert show];
Run Code Online (Sandbox Code Playgroud)

然后像这样处理它:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
// The user created a new item, add it
if (buttonIndex == 1) {
    // Get the input text
    NSString *newItem = [[alertView textFieldAtIndex:0] text];
}
}
Run Code Online (Sandbox Code Playgroud)

但它看起来不像clickedButtonAtIndex被调用,为什么?

亲切的问候,Erik

luc*_*isi 7

您需要设置委托.

alert.delegate = self; //Or some other object other than self
Run Code Online (Sandbox Code Playgroud)

或者,当您初始化警报时:

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"New item" 
                                                message:@"Enter a name for the item"
                                               delegate:self
                                      cancelButtonTitle:@"Cancel"
                                      otherButtonTitles:@"Add", nil];
Run Code Online (Sandbox Code Playgroud)