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??

4

2 回答 2

0

在您的 .h 文件中添加<UITextFieldDelegate>

然后在你的- (void)viewDidLoad尝试中,它对我来说很好:

    // 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];
于 2013-06-07T10:33:36.693 回答
0

尝试更换

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

[self.msgTextField setInputAccessoryView:toolbar];
于 2013-06-07T10:13:35.670 回答