UIAlertView与UIAlertController-iOS 8中无键盘
我有一个针对iOS 6及更高版本的旧Xcode项目。我最近在Xcode 6中打开了它,并在iPhone6 iOS8模拟器中运行。当我尝试这个动作时UIAlertView与UIAlertController-iOS 8中无键盘,ios,objective-c,xcode,ios-simulator,Ios,Objective C,Xcode,Ios Simulator,我有一个针对iOS 6及更高版本的旧Xcode项目。我最近在Xcode 6中打开了它,并在iPhone6 iOS8模拟器中运行。当我尝试这个动作时 UIAlertView* dialog = [[UIAlertView alloc] initWithTitle:@"Enter Folder Name" message:@"Keep it short and sweet"
UIAlertView* dialog = [[UIAlertView alloc] initWithTitle:@"Enter Folder Name"
message:@"Keep it short and sweet"
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"OK", nil];
dialog.alertViewStyle = UIAlertViewStylePlainTextInput;
dialog.tag = 400;
[dialog show];
我得到一个弹出窗口,但当我点击文本字段时,没有键盘出现。我在谷歌上搜索到我需要使用UIAlertController。因为我需要支持iOS 6、7版本,所以我像这样修改了代码
if ([UIAlertController class])
{
// use UIAlertController
UIAlertController *alert= [UIAlertController
alertControllerWithTitle:@"Enter Folder Name"
message:@"Keep it short and sweet"
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action){
//Do Some action here
UITextField *textField = alert.textFields[0];
NSLog(@"text was %@", textField.text);
}];
UIAlertAction* cancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
NSLog(@"cancel btn");
[alert dismissViewControllerAnimated:YES completion:nil];
}];
[alert addAction:ok];
[alert addAction:cancel];
[alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder = @"folder name";
textField.keyboardType = UIKeyboardTypeDefault;
}];
[self presentViewController:alert animated:YES completion:nil];
}
else
{
// use UIAlertView
UIAlertView* dialog = [[UIAlertView alloc] initWithTitle:@"Enter Folder Name"
message:@"Keep it short and sweet"
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"OK", nil];
dialog.alertViewStyle = UIAlertViewStylePlainTextInput;
dialog.tag = 400;
[dialog show];
}
如果我再次尝试相同的操作,则不会显示键盘
这是Xcode 6模拟器中的错误还是我做错了什么
我对此进行了测试,如果iOS8检测到硬件键盘,则不会打开键盘。因为你们可能在模拟器中测试,所以它并没有显示任何键盘 按“命令”+“k”,您应该能够看到键盘 在设备上进行测试时,您不会遇到此问题,除非用户已将其设备与硬件蓝牙键盘连接,所以这不是您应该担心的问题
希望这有帮助另一种看待键盘的方式是设置键盘类型。这可能是一个示例代码:
UIAlertController* alertController = [UIAlertController
alertControllerWithTitle:@"Test"
message:@"Testing keyboard"
preferredStyle:UIAlertControllerStyleAlert];
[alertController addTextFieldWithConfigurationHandler: ^(UITextField *tf)
{
tf.autocorrectionType = UITextAutocorrectionTypeYes;
tf.keyboardType = UIKeyboardTypeDefault;
}];
[self presentViewController:alertController animated:YES completion:nil];
然后,当您显示AlertController时,您将看到模拟器的键盘尝试按“Command”+“K”,可能是因为iOS检测到硬件键盘,所以在模拟器菜单选项“硬件-->键盘-->切换软件键盘”中未显示键盘感谢dude检查此项。我会记下你的答案。还有一个问题。如果我只在iOS 8设备上使用UIAlertView,该代码会不会不起作用?我在模拟器中尝试过,它确实可以工作,但它被贬值了,所以苹果可能会在即将发布的iOS版本中停止这一点,所以最好不要使用去擦伤的方法,保持最新,但现在应该没有问题