UIAlertViewStylePlainTextInput返回键委托 [英] UIAlertViewStylePlainTextInput return key delegate
问题描述
我正在使用UIAlertView的新iOS 5功能之一。我像这样创建一个UIAlertView:
I'm using one of the new iOS 5 features for a UIAlertView. I create a UIAlertView like this:
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
}
}
}
}
现在我想模拟回车键,所以当用户点击键盘返回时,按下警报的OK按钮就会发生同样的事情。我该怎么做?
Now I want to simulate the enter key, so when the user hits return on the keyboard the same thing happens when pressing the OK button of the alert. How can I do this?
提前致谢!
推荐答案
确保您的班级符合< UITextFieldDelegate>
协议,为您的班级设置 UIAlertView
属性并添加下面一行到您的设置代码......
Make sure your class conforms to the <UITextFieldDelegate>
protocol, make the UIAlertView
a property for your class and add the following line to your setup code...
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文件中,你实现下面的委托方法并告诉警报消失:
By becoming the delegate for the input text field you can find out when the return key on the keyboard is pressed. Then in the .m file for your class you implement the delegate method below and tell the alert to disappear:
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
[self.scanCode dismissWithClickedButtonIndex:self.scanCode.firstOtherButtonIndex animated:YES];
return YES;
}
这篇关于UIAlertViewStylePlainTextInput返回键委托的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!