UIBarButtonItem does not respond to action event - ios

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

Related

Add a ToolBar Done Button for Picker

I am trying to add a done button on the top of the picker as follows. But unfortonately, I could not able to see done button.
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc]
initWithTitle:#"Done" style:UIBarButtonItemStyleDone
target:self action:#selector(done)];
UIToolbar *toolBar = [[UIToolbar alloc]initWithFrame:
CGRectMake(0, self.view.frame.size.height-
picker.frame.size.height-250, self.view.frame.size.width, 50)];
[toolBar setBarStyle:UIBarStyleBlackOpaque];
NSArray *toolbarItems = [NSArray arrayWithObjects:
doneButton, nil];
[toolBar setItems:toolbarItems];
categoryTF.inputView = picker;
You forgot to add your toolBar into a view. You can do that as follows:
[self.view addSubview:toolBar];
add this line:
categoryTF.inputAccessoryView = toolBar;
Use toolbar and done button for pickerView
CGRect pickerFrame = CGRectMake(0,kSCREEN_HEIGHT-200,kSCREEN_WIDTH,200);
UIPickerView * pickerview = [[UIPickerView alloc] initWithFrame:pickerFrame];
pickerview.delegate = self;
pickerview.dataSource = self;
textField.inputView=pickerview;
UIToolbar *myToolbar=[[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, kSCREEN_WIDTH, 56)];
myToolbar.barStyle=UIBarStyleBlack;
[myToolbar sizeToFit];
myToolbar.backgroundColor=[UIColor whiteColor];
NSMutableArray *barItems=[[NSMutableArray alloc]init];
UIBarButtonItem *btnItem=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
[barItems addObject:btnItem];
UIBarButtonItem *doneBtn=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(pickerDoneClicked)];
[barItems addObject:doneBtn];
[myToolbar setItems:barItems animated:YES];
myToolbar.barStyle = UIBarButtonItemStylePlain;
myToolbar.barTintColor = [UIColor colorWithRed:0.94f green:0.94f blue:0.96f alpha:1.0f];
myToolbar.tintColor=[UIColor blackColor];
txtField.inputAccessoryView=myToolbar;
-(void)pickerDoneClicked
{
[txtField resignFirstResponder];
}

The action of UIBarbuttonItem on UIToolBar not called

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?

UITextField delegate causes UIToolBar not show in iOS

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??
Try replacing
[inputAccView addSubview:toolbar];
[self.msgTextField setInputAccessoryView:inputAccView];
with
[self.msgTextField setInputAccessoryView:toolbar];
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];

UIBarButtonItem not firing its action method

In viewDidLoad:
- (void)viewDidLoad
{
[super viewDidLoad];
[self.navigationController setNavigationBarHidden:YES animated:NO];
UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
NSMutableArray *toolBarItems = [[NSMutableArray alloc] init];
[toolBarItems addObject:[[UIBarButtonItem alloc] initWithTitle:#"Articles" style:UIBarButtonItemStyleBordered target:self action:#selector(backButtonTapped)]];
[toolBarItems addObject:[[UIBarButtonItem alloc] initWithTitle:#"Source" style:UIBarButtonItemStyleBordered target:self action:nil]];
[toolBarItems addObject:[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil]];
[toolBarItems addObject:[[UIBarButtonItem alloc] initWithTitle:#"Aa" style:UIBarButtonItemStyleBordered target:self action:nil]];
[toolBarItems addObject:[[UIBarButtonItem alloc] initWithTitle:#"Rabbit" style:UIBarButtonItemStyleBordered target:self action:nil]];
[toolBar setBackgroundImage:[[UIImage alloc] init] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
toolBar.items = toolBarItems;
[self.view addSubview:toolBar];
Method:
- (void)backButtonTapped {
[self.navigationController popViewControllerAnimated:YES];
}
I have a breakpoint on the call inside the method but it never gets called. Why is this method never getting called?
I had a UITapGestureRecognizer on the whole view that intercepted the tap on the UIBarButton. I solved it thanks to this answer, which basically stopped the UITapGestureRecognizer from beginning unless it was outside of the UIToolBar.
Your code is not complete and I dont know if you didn't implement it or just didn't wrote it here, so just to make sure, this should work:
UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
NSMutableArray *toolBarItems = [[NSMutableArray alloc] init];
[toolBarItems addObject:[[UIBarButtonItem alloc] initWithTitle:#"Articles" style:UIBarButtonItemStyleBordered target:self action:#selector(backButtonTapped)]];
toolBar.items = toolBarItems;
[self.view addSubview:toolBar];
Try like this. Its working fine.
UIToolbar *toolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
UIBarButtonItem *infoButton = [[UIBarButtonItem alloc]initWithTitle:#"back"style:UIBarButtonItemStyleBordered target:self action:#selector(backButtonTapped)];
[toolbar setItems:[NSArray arrayWithObjects:infoButton,nil]];
[self.view addSubview:toolbar];

cancel button appears but done button does not appear in uitoolbar

I have written the code below to display the datepicker in popover along uitoolbar.
I can see the cancel button but not the Done button.
If I remove the code for flexspace then done button appears but I need the done button to be on the extreme right positon but it stands next to cancel button.
How can I fix this ? Thanks in Advance.
UIViewController* popoverContent = [[UIViewController alloc] init]; //ViewController
UIView *popoverView = [[UIView alloc] init]; //view
popoverView.backgroundColor = [UIColor blackColor];
datePicker=[[UIDatePicker alloc]init];//Date picker
datePicker.frame=CGRectMake(0,44,320, 216);
datePicker.datePickerMode = UIDatePickerModeDate;
[datePicker setMinuteInterval:5];
[datePicker setTag:10];
[datePicker addTarget:self action:#selector(Result) forControlEvents:UIControlEventValueChanged];
[popoverView addSubview:datePicker];
popoverContent.view = popoverView;
popoverController = [[UIPopoverController alloc] initWithContentViewController:popoverContent];
popoverController.delegate=self;
[popoverController setPopoverContentSize:CGSizeMake(320, 264) animated:NO];
[popoverController presentPopoverFromRect:self.uitext.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];//tempButton.frame where you need you can put that frame
// UIToolbar *pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0,320,40)];
// [pickerToolbar sizeToFit];
// pickerToolbar.barStyle = UIBarStyleBlackTranslucent;
// NSMutableArray *barItems = [[NSMutableArray alloc] init];
//
// UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithTitle:#"Cancel" style:UIBarButtonSystemItemCancel target:self action:#selector(cancel_clicked:)];
// [barItems addObject:cancelBtn];
//
// UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
// [barItems addObject:flexSpace];
//
// UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithTitle:#"Done" style:UIBarButtonItemStyleDone target:self action:#selector(done_clicked:)];
// [barItems addObject:doneBtn];
//
//
//
// [pickerToolbar setItems:barItems animated:YES];
//
// UIPickerView *picker = [[UIPickerView alloc] init];
// picker.frame = CGRectMake(0, 44, 320, 216);
// picker.delegate = self;
// picker.dataSource = self;
// picker.showsSelectionIndicator = YES;
// [actionSheet addSubview:picker];
UIToolbar *pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0,self.view.bounds.size.width,40)];
[pickerToolbar sizeToFit];
pickerToolbar.barStyle = UIBarStyleBlackTranslucent;
NSMutableArray *barItems = [[NSMutableArray alloc] init];
UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithTitle:#"Cancel" style:UIBarButtonSystemItemCancel target:self action:#selector(cancel_clicked:)];
[barItems addObject:cancelBtn];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[barItems addObject:flexSpace];
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithTitle:#"Done" style:UIBarButtonItemStyleDone target:self action:#selector(done_clicked:)];
[barItems addObject:doneBtn];
[pickerToolbar setItems:barItems animated:YES];
[popoverView addSubview:pickerToolbar];
[self.uitext resignFirstResponder];
Try this code :-
UIToolbar *pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0,self.view.bounds.size.width,40)];
[pickerToolbar sizeToFit];
pickerToolbar.barStyle = UIBarStyleBlackTranslucent;
NSMutableArray *barItems = [[NSMutableArray alloc] init];
UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithTitle:#"Cancel" style:UIBarButtonSystemItemCancel target:self action:#selector(cancel_clicked:)];
[barItems addObject:cancelBtn];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[barItems addObject:flexSpace];
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithTitle:#"Done" style:UIBarButtonItemStyleDone target:self action:#selector(done_clicked:)];
[barItems addObject:doneBtn];
[pickerToolbar setItems:barItems animated:YES];
[popoverView addSubview:pickerToolbar];
Hope it helps you..
EDIT :-
Add this line below [pickerToolbar setItems:barItems animated:YES]; :-
pickerToolbar.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleRightMargin;
and comment [pickerToolbar sizeToFit]; line
So your code will be
UIToolbar *pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0,self.view.bounds.size.width,44.0)];
//[pickerToolbar sizeToFit];
pickerToolbar.barStyle = UIBarStyleBlackTranslucent;
NSMutableArray *barItems = [[NSMutableArray alloc] init];
UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithTitle:#"Cancel" style:UIBarButtonSystemItemCancel target:self action:#selector(cancel_clicked:)];
[barItems addObject:cancelBtn];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[barItems addObject:flexSpace];
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithTitle:#"Done" style:UIBarButtonItemStyleDone target:self action:#selector(done_clicked:)];
[barItems addObject:doneBtn];
[pickerToolbar setItems:barItems animated:YES];
pickerToolbar.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleRightMargin;
[popoverView addSubview:pickerToolbar];

Resources