0

This is what I am doing to display a UIToolbar in a UITextField's acceccory view.. Unfortunately I am not able to see it for some reason. What am I doing wrong here?

UIBarButtonItem *flexiableItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
    UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneTyping)];

    UIBarButtonItem *item2 =  [[UIBarButtonItem alloc] initWithTitle:@"Next" style:UIBarButtonItemStyleBordered
                                                              target:self  action:@selector(gotoNextTextfield:)];



    UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];


    NSArray *items = [NSArray arrayWithObjects:item2, flexiableItem, item1, nil];
    toolbar.items = items;

    [inputAccView addSubview:toolbar];

    [self.msgTextField setInputAccessoryView:inputAccView];

Update:

if I remove self.msgTextField.delegate = self; then I can see the toolbar.. but why??

Tim Tuffley
  • 605
  • 1
  • 6
  • 20

2 Answers2

0

Try replacing

[inputAccView addSubview:toolbar];
[self.msgTextField setInputAccessoryView:inputAccView];

with

[self.msgTextField setInputAccessoryView:toolbar];
Adithya
  • 4,545
  • 3
  • 25
  • 28
0

In your .h file add <UITextFieldDelegate>

Then in your - (void)viewDidLoad try This, it works fine for me:

    // Keyboard Tool Bar

    UIToolbar *toolbar = [[UIToolbar alloc] init];
    [toolbar setBarStyle:UIBarStyleBlackTranslucent];
    [toolbar sizeToFit];


    UIBarButtonItem *nextField =[[UIBarButtonItem alloc] initWithTitle:@"Next" style:UIBarButtonItemStyleBordered target:self action:@selector(nextField)];
    UIBarButtonItem *flexButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
    UIBarButtonItem *doneButton =[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(resignKeyboard)];

    NSArray *itemsArray = [NSArray arrayWithObjects: nextField, flexButton, doneButton, nil];

    [toolbar setItems:itemsArray];

    [self.msgTextField setInputAccessoryView:toolbar];
iMubarak
  • 462
  • 5
  • 13
  • I have a weird problem.. if I remove `self.msgTextField.delegate = self;` then I can see the toolbar! I dont get it.. – Tim Tuffley Jun 07 '13 at 23:13