加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

iphone – UIAlertViewStylePlainTextInput返回密钥委托

发布时间:2020-12-15 01:39:03 所属栏目:百科 来源:网络整理
导读:我正在使用一个新的iOS 5功能的UIAlertView.我创建一个这样的UIAlertView: UIAlertView *scanCode = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Some Title",@"") message:nil delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:
我正在使用一个新的iOS 5功能的UIAlertView.我创建一个这样的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
            }
        }
    }
}

现在我想模拟输入键,所以当用户在键盘上返回时,同样的事情发生在按下警报的确定按钮.我该怎么做?

提前致谢!

解决方法

确保你的类符合< UITextFieldDelegate>协议,使UIAlertView为您的类的属性,并添加以下行到您的设置代码…

self.scanCode = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Some Title",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;
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读