0

I've subclassed UIPickerView to add some a little more functionality(I'm 99% sure it has nothing to do with this question). In drawRect I added a toolbar to make dismissing the toolbar a little easier, the problem is, neither the UIToolbar, or the UIBarButtonItem inside the toolbar receive touches. It's almost as if the view is "invisible" in that the touches are forwarded to the view behind it(a UITableView). I know I could just make a "control" view that holds both the picker an a toolbar. But I just wanted to know if there was any way to do this without creating another view?

Here's my drawRect code:

- (void)drawRect:(CGRect)rect
{

    if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {

        pickerToolbar = [[UIToolbar alloc] initWithFrame: CGRectMake(0, 0, self.frame.size.width,  40)];


        UIBarButtonItem *closeButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemDone target: self.delegate action: closeAction];

        [pickerToolbar setItems: [NSArray arrayWithObject: closeButton]];

        [self addSubview: pickerToolbar];
    }
}

Here's a photo:

My custom UIPicker

Kanan Vora
  • 2,124
  • 1
  • 16
  • 26
pob21
  • 1,918
  • 2
  • 16
  • 17

1 Answers1

1

Never, never do this. You are adding a new subview every time the picker is drawn!

If your picker view is the input view of a text field, simply create a toolbar and add it as the input accessory view. It will then appear above the picker for you. This is the standard way to achieve this behaviour.

jrturton
  • 118,105
  • 32
  • 252
  • 268
  • Now that I think about it, you're absolutely rite, that's really really stupid haha. But the input accessory view is readonly, so I'm guessing i just redefine the property. I'll try that out, thanks! – pob21 Mar 21 '12 at 23:05
  • This actually didn't give me the result I was looking for, but thanks a lot for your help! – pob21 Mar 22 '12 at 02:23
  • 1
    Input accessory view isn't read only. Perhaps I wasnt clear. You have a text field you are using the picker to edit the contents of. This textfield has two properties - input view (your standard UIPickerView view) and input accessory view (your toolbar). Set these and the system puts them together for you. Why are you subclassing the picker? – jrturton Mar 22 '12 at 06:35
  • Sorry for the late reply but no you're were totally clear, it's just initially I thought you were talking about the UIPickerView's inputAccessoryView but when I edited my code I used UITextfields, which didn't give me the result I was looking for. And I'm subclassing it for other reasons, my app is pretty large and subclassing it allowed me to save myself from writing a TON of code. Thanks for the answer. – pob21 Mar 27 '12 at 02:36