【问题标题】:UITextFieldDelegate works on iOS 8 but not iOS 7UITextFieldDelegate 适用于 iOS 8 但不适用于 iOS 7
【发布时间】:2015-01-30 17:13:02
【问题描述】:

我有一个 UIViewController,里面有一个 UITextField,并且符合 UITextFieldDelegate 协议。当我第一次构建应用程序时,iOS 7 是最新的 iOS,当我选择文本字段时,键盘会按预期出现。

随之而来的是 iOS 8 和 Xcode 6。自从我第一次编写代码以来,代码没有任何变化,但现在,奇怪的是,当我在 iOS 8 设备上选择文本字段时,小键盘会出现,但在 iOS 7 设备上却会出现不是。

为什么会这样?有什么想法吗?

这是我的代码:

#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

【问题讨论】:

    标签: ios7 ios8 uitextfield uitextfielddelegate


    【解决方案1】:

    如果有的话,就是一条红鲱鱼。事实证明,这是 iOS 模拟器检测到硬件键盘的情况,因此没有提升模拟的 iPhone 键盘。我认为代理在 iOS 8 而不是 iOS 7 上工作的原因是因为我的设备运行 iOS 8,所以当我测试那个版本的 iOS 时我会在设备上进行测试,当我需要测试 iOS 7 时我使用了模拟器. 一旦我在模拟器上测试了 iOS 8,我就发现了我的错误假设,我突然意识到,区别不在于 iOS 版本之间,而在于设备和模拟器之间。我通过进入模拟器纠正了这个问题,然后进入硬件菜单,然后是键盘,然后取消选中“连接硬件键盘”选项。

    这让我很沮丧。我希望有人能从我的帖子中受益!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-07-19
      • 1970-01-01
      • 1970-01-01
      • 2015-01-11
      • 2014-04-06
      • 1970-01-01
      • 1970-01-01
      • 2015-01-01
      相关资源
      最近更新 更多