Search code examples
iosuitextfielduipopovercontrolleruitextfielddelegate

Unable to display popover in iOS from UITextField


I am working on a very basic app that displays a popover when the user is entering text into a UITextField. Unfortunately, the popover is not showing up and the default keyboard is appearing (which shouldn't). Here is my relevant code below:

NumberPadViewController.h

    #import <UIKit/UIKit.h>
    #import "NumberViewController.h"

    @interface NumberPadViewController : UIViewController <UITextFieldDelegate> {

        IBOutlet UITextField *numTest;

    }

    @property (nonatomic, strong) NumberViewController *numberPicker;
    @property (nonatomic, strong) UIPopoverController *numberPickerPopover;

@end

NumberPadViewController.m

- (BOOL)textFieldShouldBeginEditing:(UITextField *) textField
{
    // Create popover controller if nil

    if(_numberPickerPopover == nil){   //make sure popover isn't displayed more than once in the view

        _numberPickerPopover = [[UIPopoverController alloc]initWithContentViewController:_numberPicker];

    }

    [_numberPickerPopover presentPopoverFromRect:numTest.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
    return NO;

}

My popover class is called NumberViewController.h

@interface NumberViewController : UIViewController {


}

@property (strong, nonatomic) IBOutlet UIButton *oneButton;

NumberViewController.m

#import "NumberViewController.h"

@interface NumberViewController ()

@end

@implementation NumberViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization

        NSInteger buttonHeight = _oneButton.frame.size.height * 4;
        NSInteger buttonWidth = _oneButton.frame.size.width * 3;
        self.contentSizeForViewInPopover = CGSizeMake(buttonWidth, buttonHeight);

    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

I have created the UITextField in Storyboard, and set the delegate there. Can anyone see what I am doing wrong?

Thanks in advance to all who reply.


Solution

  • Ensure the textFieldShouldBeginEditing delegate method is being called. The rest of the code looks correct.