Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Google Sign In with OpenID
Please do not post the same thing multiple times. The board software automatically flags certain posts as needing moderator attention. This happens the most often for new users. I'm pretty sure this is made clear at the time you attempt to post. Posting the same thing over and over again just makes that many more posts the moderators have to weed through later. This makes us sad. Don't make us sad. If your post/thread doesn't appear, just wait a while. Don't post it again. If it hasn't shown up by the next day, then you can try again. I normally go through posts in the mornings, and try to check a few times throughout the day, but I'm not here 24/7. There will typically be a significant delay before posts are approved. Just be patient.

Programmatically add picker to tableview with same behavior as default keyboard

I have a tableview with a few cells. One of these contains a textfield, which shows the keyboard when selected.

Now in the tableviewcontroller class I create a UIPickerView in the viewWillAppear method, like this:

moodPickerView = [[UIPickerView alloc] initWithFrame:CGRectZero];
moodPickerView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
CGSize pickerSize = [moodPickerView sizeThatFits:CGSizeZero];
moodPickerView.frame = [self pickerFrameWithSize:pickerSize];
moodPickerView.showsSelectionIndicator = YES; // note this is default to NO
moodPickerView.dataSource = self;
moodPickerView.delegate = self;
moodPickerView.hidden = YES;
[self.view addSubview:moodPickerView];


When a certain cell is selected I want to show the picker, like so:

[picker setCenter:CGPointMake(self.view.frame.size.width/2, self.view.frame.size.height+(picker.frame.size.height/2))]; // place the pickerView outside the screen boundaries
[picker setHidden:NO]; // set it to visible and then animate it to slide up
[UIView beginAnimations:@\"slideIn\" context:nil];
[picker setCenter:CGPointMake(self.view.frame.size.width/2, self.view.frame.size.height-(picker.frame.size.height/2))];
[UIView commitAnimations];



While the keyboard is up, I can still scroll in the tableview while the keyboard stays at the bottom of the screen. But, when the pickerview is up I can't.

How do I show the pickerview so it has the same behavior as the keyboard?
Post edited by edwinvanolst on

Replies

  • smithdale87smithdale87 Posts: 4,293iPhone Dev SDK Supporter
    First off, you're doing it wrong if you're adding subviews in viewWillAppear. You should be using viewDidLoad to create your picker instance and add it to the subview.

    Can you NSLog the frame of your picker when you are showing it? I suspect the picker's frame is likely the entire screen, which means it would be blocking touches from going to the tableview.
  • DomeleDomele Posts: 2,948Registered Users
    UITextFields have an inputView property. This replaces the keyboard. Set this property in cellForRow.
    If you are looking for a quality developer, I'm your man. Give me a PM if you are interested.



    New app - See screenshots and details at www.globaclock.com.



    If you want to
  • edwinvanolstedwinvanolst Posts: 7New Users
    smithdale87;427429 said:
    First off, you're doing it wrong if you're adding subviews in viewWillAppear. You should be using viewDidLoad to create your picker instance and add it to the subview.
    Thanks for this, I was wondering about this myself.
    smithdale87;427429 said:
    Can you NSLog the frame of your picker when you are showing it? I suspect the picker's frame is likely the entire screen, which means it would be blocking touches from going to the tableview.
    NSLog(@\"picker view frame: %f x %f\", picker.frame.size.width, picker.frame.size.height);

    returns: picker view frame: 320.000000 x 216.000000

    The touches aren't being blocked as I can select the row above it.
  • edwinvanolstedwinvanolst Posts: 7New Users
    Domele;427430 said:
    UITextFields have an inputView property. This replaces the keyboard. Set this property in cellForRow.
    I'm a bit confused by this. The cell with the pickerview doesn't have a textfield, just a label that get's updated when the picker value changes. Also, I currently don't use cellForRow. The tableview uses static cells, using storyboard/ib.
  • DomeleDomele Posts: 2,948Registered Users

    One of these contains a textlabel, which shows the keyboard when selected.
    You say that you have a text label. I assume it's a UITextField since it shows a keyboard when selected. UITextFields have an inputView property which you can set to a view. In your case, you should set this property to your UIPickerView. You should add yourself as a target for UIControlEventValueChanged events from the UIPickerView. In the method that gets called when the value changes, update the UITextField.
    If you are looking for a quality developer, I'm your man. Give me a PM if you are interested.



    New app - See screenshots and details at www.globaclock.com.



    If you want to
  • edwinvanolstedwinvanolst Posts: 7New Users
    Domele;427521 said:
    You say that you have a text label. I assume it's a UITextField since it shows a keyboard when selected.
    Yes you're right, I meant textfield (updated my post). But the textfield is in a different cell, one that requires the keyboard. The other cell however has a UILabel and shows a pickerview when selected (via didSelectRowAtIndexPath).
  • DomeleDomele Posts: 2,948Registered Users
    So why not just replace it with a UITextField? It's the same thing except it summons a keyboard automatically and you can replace that keyboard with your own view.

    And to get the same functionality of clicking anywhere in the cell to bring up the UIPickerView, just call becomeFirstResponder on the UITextField in didSelectRow.
    If you are looking for a quality developer, I'm your man. Give me a PM if you are interested.



    New app - See screenshots and details at www.globaclock.com.



    If you want to
  • edwinvanolstedwinvanolst Posts: 7New Users
    Domele;427528 said:
    So why not just replace it with a UITextField? It's the same thing except it summons a keyboard automatically and you can replace that keyboard with your own view.

    And to get the same functionality of clicking anywhere in the cell to bring up the UIPickerView, just call becomeFirstResponder on the UITextField in didSelectRow.
    Yeah, this seems to be the way to go. Can I hide the blinking cursor in the textfields? It's weird to see the cursor when selecting from a picker.
  • DomeleDomele Posts: 2,948Registered Users
    You'd have to switch between 2 UITextFields or something. You can look into it more by googling.
    If you are looking for a quality developer, I'm your man. Give me a PM if you are interested.



    New app - See screenshots and details at www.globaclock.com.



    If you want to
  • edwinvanolstedwinvanolst Posts: 7New Users
    Domele;427535 said:
    You'd have to switch between 2 UITextFields or something. You can look into it more by googling.
    Yeah, thanks, I solved this issue by using becomeFirstResponder in didSelectRow on a hidden uitextfield.

    Thank you for your help!
Sign In or Register to comment.