UITextFieldDelegate适用于iOS 8但不适用于iOS 7 [英] UITextFieldDelegate works on iOS 8 but not iOS 7
问题描述
我有一个UIViewController,里面有一个UITextField,符合UITextFieldDelegate协议。当我第一次构建应用程序时,iOS 7是最新的iOS,当我选择文本字段时,键盘会出现,如预期的那样。
I have a UIViewController that has a UITextField in it, and conforms to the UITextFieldDelegate protocol. When I first built the app iOS 7 was the latest iOS and when I selected the text field, the keypad would appear, as expected.
iOS 8和Xcode 6.自从我第一次编写代码以来,代码没有任何变化,但现在,奇怪的是,当我在iOS 8设备上选择文本字段时键盘出现,但在iOS 7设备上它没有。
Along comes iOS 8 and Xcode 6. Nothing changed in the code since I first wrote it, but now, curiously, when I select the text field on an iOS 8 device the keypad appears, but on an iOS 7 device it does not.
为什么会这样?有什么想法吗?
Why would this be? Any ideas?
这是我的代码:
#import "BBFeatVC.h"
@interface BBFeatVC ()
@end
@implementation BBFeatVC
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.featTextField.delegate = self;
// Set label
self.featLabel.numberOfLines = 0;
// enhance keypad
UIToolbar* numberToolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
numberToolbar.barStyle = UIBarStyleBlackTranslucent;
numberToolbar.items = [NSArray arrayWithObjects:
[[UIBarButtonItem alloc]initWithTitle:@"Cancel" style:UIBarButtonItemStyleBordered target:self action:@selector(cancelNumberPad)],
[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
[[UIBarButtonItem alloc]initWithTitle:@"Apply" style:UIBarButtonItemStyleDone target:self action:@selector(doneWithNumberPad)],
nil];
[numberToolbar sizeToFit];
self.featTextField.inputAccessoryView = numberToolbar;
}
-(void)cancelNumberPad{
[self.featTextField resignFirstResponder];
}
-(void)doneWithNumberPad{
NSString *numberFromTheKeyboard = self.featTextField.text;
NSNumberFormatter *f = [[NSNumberFormatter alloc] init];
[f setNumberStyle:NSNumberFormatterDecimalStyle];
NSNumber *num = [f numberFromString:numberFromTheKeyboard];
NSInteger newStat = [num integerValue];
[[NSUserDefaults standardUserDefaults] setInteger:newStat forKey:self.featStat];
[self.featTextField resignFirstResponder];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
推荐答案
红鲱鱼,如果有的话。事实证明,这是iOS模拟器检测硬件键盘的情况,因此不会提升模拟的iPhone键盘。我认为代表在iOS 8而不是iOS 7上工作的原因是因为我的设备运行iOS 8,所以当我测试那个版本的iOS时我会测试设备,当我需要测试iOS 7时我使用了模拟器一旦我在模拟器上测试了iOS 8,我发现了我的错误假设,我突然意识到iOS版本之间的区别不是设备和模拟器之间的区别。我通过进入模拟器,然后在硬件菜单,然后键盘,然后取消选中连接硬件键盘选项来更正问题。
A red herring if there ever was one. As it turns out this was a case of the iOS Simulator detecting the hardware keyboard and therefore not raising the simulated iPhone keypad. The reason I thought the delegate was working on iOS 8 and not iOS 7 is because my device runs iOS 8 and so I would test on the device when I tested that version of iOS, and I used the simulator when I needed to test iOS 7. Once I tested iOS 8 on the simulator I discovered my flawed assumption and it dawned on me the difference was not between iOS versions but between device and simulator. I corrected the problem by going into the Simulator, and then in the Hardware Menu, then Keyboard, then uncheck the "Connect Hardware Keyboard" option.
这让我感到沮丧。我希望有人从我的帖子中受益!
This frustrated me to no end. I hope someone benefits from my posting!
这篇关于UITextFieldDelegate适用于iOS 8但不适用于iOS 7的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!