Question

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.

Était-ce utile?

La solution

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

Autres conseils

This is just a thought, but when a popover does a popover thing i think it becomes first responder, but your text view is first responder... so it might not be able to over do it.... if this is the error then before you tell the popover to appear you can say [textView resignFirstResponder]; and see if that helps.... it's just a thought though not 100% sure i will have to do some testing ~

also check to see if _numberPicker is not nil aswell i don't know what happens if you try to display a popover with no controller but you can see if that's the problem

Seeing as your popover isn't represented in the storyboard (if I read your post right) I think you need to add the popover view as a subview in code. Something like:

[self addSubview:_numberPickerPopover];

There are potentially a few places to do this. Probably makes the most sense in your textFieldShouldBeginEditing: method, after you've inited it.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top