ios - The action of UIBarbuttonItem on UIToolBar not called -
i having trouble action of uibarbuttonitem on uitoolbar not called.
in following code, although donebtn
on toolbar
tapped, action donebtnaction:
not called.
have idea fix it?
- (void)viewdidload { uipickerview *pickerview = [[uipickerview alloc] init]; uitoolbar *toolbar = [[uitoolbar alloc] initwithframe:cgrectmake(0, -44, 320, 44)]; uibarbuttonitem *donebtn = [[uibarbuttonitem alloc] initwithtitle:@"done" style:uibarbuttonitemstyledone target:self action:@selector(donebtnaction:)]; uibarbuttonitem *flex = [[uibarbuttonitem alloc] initwithbarbuttonsystemitem:uibarbuttonsystemitemflexiblespace target:nil action:nil]; toolbar.items = @[flex, donebtn]; [pickerview addsubview:toolbar]; uitextfield *textfield = [[uitextfield alloc] init]; textfield.inputview = pickerview; } - (void)donebtnaction:(uibarbuttonitem *)sender { nslog(@"%@", sender); }
don't add toolbar subview of picker view, negative y origin (no touches reach toolbar because taps clipped picker view's frame).
instead, make toolbar inputaccessoryview
of text field.
textfield.inputaccessoryview = toolbar;
complete code:
- (void)viewdidload { uipickerview *pickerview = [[uipickerview alloc] init]; uitoolbar *toolbar = [[uitoolbar alloc] initwithframe:cgrectmake(0, 0, 320, 44)]; uibarbuttonitem *donebtn = [[uibarbuttonitem alloc] initwithtitle:@"done" style:uibarbuttonitemstyledone target:self action:@selector(donebtnaction:)]; uibarbuttonitem *flex = [[uibarbuttonitem alloc] initwithbarbuttonsystemitem:uibarbuttonsystemitemflexiblespace target:nil action:nil]; toolbar.items = @[flex, donebtn]; uitextfield *textfield = [[uitextfield alloc] init]; textfield.inputview = pickerview; textfield.inputaccessoryview = toolbar; }
one other note - why not use standard system done type bar button item?
Comments
Post a Comment