Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/43.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/113.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone UIAlertViewStylePlainTextInput返回键委托_Iphone_Ios_Xcode_Ios5 - Fatal编程技术网

Iphone UIAlertViewStylePlainTextInput返回键委托

Iphone UIAlertViewStylePlainTextInput返回键委托,iphone,ios,xcode,ios5,Iphone,Ios,Xcode,Ios5,我正在为UIAlertView使用iOS5的一个新功能。我创建的UIAlertView如下所示: UIAlertView *scanCode = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Some Title", @"") message:nil delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:NSLocalizedString(@"OK", @""), n

我正在为UIAlertView使用iOS5的一个新功能。我创建的UIAlertView如下所示:

UIAlertView *scanCode = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Some Title", @"") message:nil delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:NSLocalizedString(@"OK", @""), nil];
        [scanCode setAlertViewStyle:UIAlertViewStylePlainTextInput];
        scanCode.tag = 1234;
        [scanCode show];
        [scanCode release];
我现在使用的代理是:

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (alertView.tag == 1234) {
        if (buttonIndex == 1)
        {
            //do something
            }
        }
    }
}
现在我想模拟enter键,这样当用户在键盘上点击return时,按下警报的OK按钮时也会发生同样的事情。我该怎么做


提前谢谢

确保您的类符合
协议,将
UIAlertView
作为类的属性,并将以下行添加到设置代码中

self.scanCode = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Some Title", @"") message:nil delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:NSLocalizedString(@"OK", @""), nil];
[self.scanCode setAlertViewStyle:UIAlertViewStylePlainTextInput];
self.scanCode.tag = 1234;
//add this...
[[self.scanCode textFieldAtIndex:0] setDelegate:self];
[self.scanCode show];
通过成为输入文本字段的代理,您可以了解何时按下键盘上的返回键。然后在类的.m文件中实现下面的委托方法,并告诉警报消失:

-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [self.scanCode dismissWithClickedButtonIndex:self.scanCode.firstOtherButtonIndex animated:YES];
    return YES;
}

谢谢每天15小时的编程确实会让明显的事情被遗忘;-)@jackslash如果
UIAlertView
不是一个属性,您可以在
textFieldShouldReturn:
方法中关闭父级
UIAlertView
?(除了向上提升superview层次结构?@jackslash之外,还应该使用
self.scanCode.firstOtherButtonIndex而不是
0
)。并确保
UIAlertView
属性是一个弱引用,否则它将在它消失后保存在内存中。@zekel我认为您需要将alert视图保留为属性,以便以编程方式取消它。您可以按照您的建议向上查看视图层次结构,但我倾向于编写更清晰的代码并将其作为属性。我也同意,使用弱属性是最好的方法,但如果您打算重用警报视图,保留它与不保留它相比,没有什么惩罚,因为核心动画在其不在屏幕上时会释放备份存储。