Search code examples
ios7ios8uitextfielduitextfielddelegate

UITextFieldDelegate works on iOS 8 but not iOS 7


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.

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?

Here is my code:

#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

Solution

  • 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!