The action of UIBarbuttonItem on UIToolBar not called - ios

I am having trouble as the action of UIBarbuttonItem on UIToolBar is not be called.
In the following code, although the doneBtn on toolBar is tapped, the action doneBtnAction: is not be called.
Do you have any idea to 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 the toolbar as a subview of the picker view, especially with a negative y origin (No touches reach the toolbar because the taps are clipped to the picker view's frame).
Instead, make the toolbar the inputAccessoryView of the 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 the standard system Done type for the bar button item?

Related

How to add camera button in keyboard to be use as a Barcode scanner in IOS?

How to add camera button in the keyboard to be used as a Barcode scanner in IOS?
You can use a toolbar as an accessoryview of UITextfield with UIBarButtonSystemItemCamera
toolbar = [[UIToolbar alloc] init];
toolbar.barStyle = UIBarStyleBlackOpaque;
[toolbar sizeToFit];
toolbar.frame = CGRectMake(0, 436, 320, 44);
UIBarButtonItem *spaceItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
spaceItem.width = 130.0;
UIBarButtonItem *cameraItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:#selector(captureBarItemPressed:)];
NSArray *items = [NSArray arrayWithObjects: spaceItem, cameraItem, nil];
[toolbar setItems:items];
txtField.inputAccessoryView = toolbar

Right bar button does not appear

I try to add UINavigationBar programmatically and set bar button items.
I tried:
self.artificialNavBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
self.artificialNavBar.backgroundColor = [UIColor whiteColor];
UIBarButtonItem *bbiDone = [[UIBarButtonItem alloc] initWithTitle:#"Готово" style:UIBarButtonItemStyleDone target:nil action:nil];
UIBarButtonItem *bbiTry = [[UIBarButtonItem alloc] initWithTitle:#"Cancel" style:UIBarButtonItemStyleDone target:nil action:nil];
UINavigationItem *navItem = [[UINavigationItem alloc] init];
navItem.leftBarButtonItem = bbiDone;
navItem.rightBarButtonItem = bbiTry;
self.artificialNavBar.items = #[ navItem ];
[self.view addSubview:self.artificialNavBar];
However, only left bar button appears, right one is hidden. Did i miss something?
Declare
#property (nonatomic, strong) UINavigationItem *navItem;
in the .h file of this class where you have written all these and then replace your code with below code.
self.artificialNavBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
self.artificialNavBar.backgroundColor = [UIColor whiteColor];
[self.view addSubview:self.artificialNavBar];
UIBarButtonItem *bbiDone = [[UIBarButtonItem alloc] initWithTitle:#"Готово" style:UIBarButtonItemStyleDone target:nil action:nil];
UIBarButtonItem *bbiTry = [[UIBarButtonItem alloc] initWithTitle:#"Cancel" style:UIBarButtonItemStyleDone target:nil action:nil];
self.navItem = [[UINavigationItem alloc] init];
[self.navItem setLeftBarButtonItem:bbiDone animated:NO]
[self.navItem setRightBarButtonItem:bbiTry animated:NO]
[self.artificialNavBar setItems:#[self.navItem] animated:NO]
and make sure the navbar is nonatomic and strong.

How to make a UIBarButtonItem selectable on a UIToolbar part of a UIPickerView

I have a two column UIPickerView in a project that I am working on, and I successfully created the UIPickerView, UIToolBar, and UIToolBarItem programmatically. However the toolbar item isn't responding to touch inputs. I have tried on the device and in the Simulator, and the selector method is never being called / reached.
_pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 200, 320, 216)];
UIToolbar *toolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
toolBar.barStyle = UIBarStyleBlackOpaque;
UIBarButtonItem *flex = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
UIBarButtonItem *btnAddCredit = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(addCreditToUser:)];
NSLog(#"btnAddCredit %hhd",btnAddCredit.isEnabled);
[toolBar setItems:[NSArray arrayWithObjects:flex,btnAddCredit,nil]];
toolBar.userInteractionEnabled = true;
[_pickerView addSubview:toolBar];
_pickerView.delegate = self;
_pickerView.showsSelectionIndicator = YES;
[self.parentViewController.view addSubview:_pickerView];
Here's a quick animated GIF explaining what is happening,

UIBarButtonItem don't work on tap

I get some problem to trigger my toolbar button on my datepicker. My datepicker is showing perfectly but when i click on done item button nothing append in log . I don't know why it's not working.
any help would be appreciated :)
-(void) dismiss:(id){
NSLog("test");
}
-(IBAction) datePicker:(id)sender{
...
...
UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height, 320, 44)];
toolBar.tag = 1;
toolBar.barStyle = UIBarStyleBlackTranslucent;
UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(dismiss:)];
[toolBar setItems:[NSArray arrayWithObjects:spacer, doneButton, nil]];
}
I finally found an alternative, the solution is to use through a textfield like this code:
I use ARC
UIToolbar *toolBar = [UIToolbar alloc] init];
UITextField * textfield = [[UITextField alloc]init];
UIDatePicker *datePicker = [[[UIDatePicker alloc] init]];
toolbar.barStyle = UIBarStyleDefault;
datePicker.datePickerMode = UIDatePickerModeDate;
[datePicker addTarget:self action:#selector(datePickerValueChanged:) forControlEvents:UIControlEventValueChanged];
textField.inputView = datePicker;
textfield.inputAccessoryView = toolBar;
[self.view addSubview:textfield];
[textfield becomeFirstResponder];
Hope this work for you too.

UIBarButtonItem does not respond to action event

I am using a UIToolBar and have added a UIBarButtonItem to it. When I click the button it does nothing. It does not respond to action event. This is my code :
UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(x, y, width, toolBarHeight)];
[toolBar setBarStyle:UIBarStyleBlackTranslucent];
UIBarButtonItem *barButtonDone = [[UIBarButtonItem alloc] initWithTitle:#"Done"
style:UIBarButtonItemStyleBordered target:self action:#selector(changeText:)];
toolBar.items = [[NSArray alloc] initWithObjects:barButtonDone,nil];
barButtonDone.tintColor=[UIColor blackColor];
[self.view addSubview:toolBar];
Try this and see if this helps. Again your code looks fine.
// create an array for the buttons
NSMutableArray* BarbuttonsArray = [[NSMutableArray alloc] initWithCapacity:1];
UIBarButtonItem *barButtonDone = [[UIBarButtonItem alloc] initWithTitle:#"Done"
style:UIBarButtonItemStyleBordered target:self action:#selector(changeText:)];
barButtonDone.style = UIBarButtonItemStyleBordered;
[BarbuttonsArray addObject:barButtonDone];
[toolbar setItems:BarbuttonsArray animated:YES];
I literally copied and pasted your code into a test project of mine and this is what I get. The only change I did was the method name
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
[toolBar setBarStyle:UIBarStyleDefault];
UIBarButtonItem *barButtonDone = [[UIBarButtonItem alloc] initWithTitle:#"Done"
style:UIBarButtonItemStyleBordered target:self action:#selector(changeText)];
toolBar.items = [[NSArray alloc] initWithObjects:barButtonDone,nil];
barButtonDone.tintColor=[UIColor blackColor];
[self.view addSubview:toolBar];
}
-(IBAction) changeText
{
NSLog(#"changeText ...");
}
2014-01-25 13:59:15.882 001-test-proj[15596:a0b] changeText ...

Resources