【发布时间】:2014-11-22 08:17:10
【问题描述】:
我有一个针对 iOS 6 及更高版本的旧 Xcode 项目。我最近在 Xcode 6 中打开它并在 iOS 8 的 iPhone 6 模拟器中运行。当我尝试此操作时
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 模拟器中的错误还是我做错了什么?
【问题讨论】:
-
尝试按 "Command" +"K" ,可能是 iOS 正在检测硬件键盘所以它没有显示键盘
-
在模拟器菜单选项硬件--> 键盘--> 切换软件键盘
标签: ios objective-c xcode ios-simulator