我正在使用,UIAlertView
但我收到以下错误消息:
* 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“textFieldIndex (0) 超出了文本字段数组的范围”
但是我的里面没有textFields
,UIAlertView
我从来没有在某个地方访问过它们......
这是我的代码:
alertNewVersion = [[UIAlertView alloc] initWithTitle: @"Update"
message: @"There is a new version"
delegate: self
cancelButtonTitle:@"OK"
otherButtonTitles:@"Cancel",nil];
[alertNewVersion setAlertViewStyle:UIAlertViewStyleDefault];
alertNewVersion.tag = 1;
[alertNewVersion show];
但是,当我将“ delegate:self
”更改为“”时,delegate:nil
它不会因错误而崩溃,但是我的委托函数当然不起作用……知道问题是怎么回事吗?
编辑:
第二个 UIAlertView:
- (IBAction)startPushed:(id)sender {
message = [[UIAlertView alloc] initWithTitle:@"test."
message:nil
delegate:self
cancelButtonTitle:nil
otherButtonTitles:@"OK", nil];
[message setAlertViewStyle:UIAlertViewStyleLoginAndPasswordInput];
[[message textFieldAtIndex:1] setSecureTextEntry:NO];
[[message textFieldAtIndex:0] setPlaceholder:@"xx"];
[[message textFieldAtIndex:1] setPlaceholder:@"xxx"];
message.tag = 2;
[message show];
}
委托方式:
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if(alertView.tag == 1){ // alertNewVersion
if (buttonIndex == 0) { //button "OK" clicked
NSLog(@"test");
}
}
if(alertView.tag == 2){ // Message
if (buttonIndex == 0) { //button "OK" clicked
NSString *Name = [[message textFieldAtIndex:0] text];
[[NSUserDefaults standardUserDefaults] setObject:Name forKey:@"xx"];
NSString *Class = [[message textFieldAtIndex:1] text];
[[NSUserDefaults standardUserDefaults] setObject:Class forKey:@"xxx"];
}
}
}