我已成功创建了两个带有四个按钮的警报。第一个AlertView工作得很好但第二个alertView崩溃了我。这是我的代码:
-(void)showAlertWithTextField{
UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:@"info" message:@"Set Time For The Game." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:@"cancel", nil];
[alertView setTag:101];
alertView.alertViewStyle=UIAlertViewStylePlainTextInput;
[[alertView textFieldAtIndex:0] setKeyboardType:UIKeyboardTypeNumberPad];
[[alertView textFieldAtIndex:0] becomeFirstResponder];
[alertView show];
}
-(void)showexitAlertView
{
UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:@"Timeout" message:[NSString stringWithFormat:@"Your final score is: %i", runningSore] delegate:self cancelButtonTitle:@"Restart" otherButtonTitles:@"Exit", nil];
[alertView setTag:102];
[alertView show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
UITextField *field = [alertView textFieldAtIndex:0];
setTime = [field.text intValue];
NSLog(@"%d", setTime);
if (alertView.tag == 101 && buttonIndex == 0) {
//if (buttonIndex == 0){
NSLog(@"Ok");
[self setTimer];
[self countDownTimerPrepare];
[self countDownTimerIntro];
[self prepareForIntroAnimation];
[self performIntroAnimation];
[self categoriesList];
}
else if (buttonIndex == 1){
NSLog(@"Cancel");
ChooseViewController * controller = [[ChooseViewController alloc] initWithNibName:@"ChooseViewController" bundle:nil];
//controller.countdownLabel.text= score;
[self presentViewController:controller animated:YES completion:nil];
}
else if (alertView.tag==102 && buttonIndex == 0){
NSLog(@"Restart");
}
else if (buttonIndex == 1){
NSLog(@"Exit");
}
}
当我从第二个警报视图中按下按钮时,这是一个drbugging消息:
2013-09-11 17:12:06.106 Diplomatiki [2808:c07] *由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'textFieldIndex(0)超出了文本数组的范围场 * 第一次抛出调用堆栈: (0x1c42012 0x1737e7e 0x1c41deb 0xa7f0f0 0xb5d0 0xa72020 0x174b705 0x682920 0x6828b8 0x743671 0x743bcf 0x742d38 0x6b233f 0x6b2552 0x6903aa 0x681cf8 0x1fc2df9 0x1fc2ad0 0x1bb7bf5 0x1bb7962 0x1be8bb6 0x1be7f44 0x1be7e1b 0x1fc17e3 0x1fc1668 0x67f65c 0x29fd 0x2925为0x1) libc ++ abi.dylib:terminate调用抛出异常
我会提出任何建议。提前谢谢。
答案 0 :(得分:1)
崩溃是由于您的第二个警报视图没有文本字段。但是,无论点击哪个alerview,您都试图访问文本字段及clickedButtonAtIndex:
的值。
实现如:clickedButtonAtIndex
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (alertView.tag == 101)
{
UITextField *field = [alertView textFieldAtIndex:0];
setTime = [field.text intValue];
NSLog(@"%d", setTime);
if( buttonIndex == 0)
{
NSLog(@"Ok");
[self setTimer];
[self countDownTimerPrepare];
[self countDownTimerIntro];
[self prepareForIntroAnimation];
[self performIntroAnimation];
[self categoriesList];
}
else if (buttonIndex == 1)
{
NSLog(@"Cancel");
ChooseViewController * controller = [[ChooseViewController alloc] initWithNibName:@"ChooseViewController" bundle:nil];
[self presentViewController:controller animated:YES completion:nil];
}
}
else if (alertView.tag==102)
{
if(buttonIndex == 0)
{
NSLog(@"Restart");
}
else if (buttonIndex == 1)
{
NSLog(@"Exit");
}
}
}
答案 1 :(得分:1)
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
UITextField *field = [alertView textFieldAtIndex:0];
setTime = [field.text intValue];
这是错误,您的第二个alertView不包含任何textField。正如文档中所说:
在索引处检索文本字段 - 当textFieldIndex超出范围时引发NSRangeException。 索引0处的字段将是第一个文本字段(单个字段或登录字段),索引1处的字段将是密码字段。
这是一个应该有效的解决方案
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (alertView.tag == 101 && buttonIndex == 0) {
UITextField *field = [alertView textFieldAtIndex:0];
setTime = [field.text intValue];
NSLog(@"%d", setTime);
...
}
答案 2 :(得分:1)
Axel,与前一种情况相同的问题
- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView
{
if (alertView.tag == 101){
NSString *inputText = [[alertView textFieldAtIndex:0] text];
if( [inputText length] >= 1 ){
return YES;
}
else{
return NO;
}
}else{
return NO;
}
}