0

I want a UIToolbar to show up when I click on a certain UITextField, but the toolbar, defined in the @interface, doesn't show up. This is my code for initializing and attaching it to the text field:

toolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
toolbar.barStyle = UIBarStyleBlackTranslucent;
toolbar.items = [NSArray arrayWithObjects:[[UIBarButtonItem alloc]initWithTitle:@"Done" style:UIBarButtonItemStyleBordered target:self action:@selector(isDone:)], nil];
toolbar.hidden = NO;
[toolbar sizeToFit];

angleField.inputAccessoryView = toolbar;

Can someone see what I'm doing wrong?

P.S. I had used similar-looking code in another project, and it worked. Here is the code:

 UIToolbar* numberToolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
numberToolbar.barStyle = UIBarStyleBlackTranslucent;
numberToolbar.items = [NSArray arrayWithObjects:
                       [[UIBarButtonItem alloc]initWithTitle:@"Clear" style:UIBarButtonItemStyleBordered target:self action:@selector(cancelNumberPad:)],
                       [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
                       [[UIBarButtonItem alloc]initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(doneWithNumberPad:)],
                       nil];
[numberToolbar sizeToFit];
changes.inputAccessoryView = numberToolbar;

where changes is a UITextField

Jeeter
  • 5,887
  • 6
  • 44
  • 67
  • have you got the delegate set for it? – Jatin Nov 06 '12 at 23:55
  • I'm not too familiar with that. I used code that was pretty much the same in another project, and it worked. I can post that code, too. – Jeeter Nov 07 '12 at 00:01
  • I don't see an `-addSubview:` in there. – CodaFi Nov 07 '12 at 00:09
  • would it be `[self addSubview: toolbar]`? – Jeeter Nov 07 '12 at 00:10
  • It may not be self. It depends on where you are trying to add the toolbar to. Is self a View? ViewController? Do you want it to be on the Navigation controller? But you do need to add the toolbar to the subview to have it appear. – jer-k Nov 07 '12 at 00:17
  • what I want to do is to have the toolbar appear with the keyboard, like how it does on the Safari app, with the "next", "previous", and "done" buttons – Jeeter Nov 07 '12 at 00:36
  • I see. What function is all your code inside of? As in, exactly where is `angleField.inputAccessoryView = toolbar;` getting called? – jer-k Nov 07 '12 at 00:50
  • my function is inside the `viewDidLoad:` method, as well as the `inputAccessoryView:` call. – Jeeter Nov 07 '12 at 20:38
  • Check if `angleField` is nil when you are doing this. – iDev Dec 24 '12 at 08:05
  • @ACB `angleField` is not nil, and when I log the description of `angleField.inputAccessoryView`, it gives me the memory location of a `UIToolbar`. – Jeeter Jan 02 '13 at 17:48

1 Answers1

0

I'm not sure if this is it, but what I did was instead of calling setItems: on the toolbar, I called setItems:animated: and now it works...

Thank you to all who responded and took the time to try to help me. Happy coding!

Jeeter
  • 5,887
  • 6
  • 44
  • 67