UIToolbar button crashes app when pressed - ios

I have a toolbar set to appear over my keyboard whenever I open a UITextView. This is my current code.
UIToolbar* numberToolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
numberToolbar.barStyle = UIBarStyleBlackTranslucent;
numberToolbar.items = [NSArray arrayWithObjects:[[UIBarButtonItem alloc]initWithTitle:#"Done" style:UIBarButtonItemStyleDone target:self action:#selector(done:)],nil];
[numberToolbar sizeToFit];
self.yourTextView.inputAccessoryView = numberToolbar;
However, when I press the button, the app crashes. How do I make the button to where when pressed, would switch the cursor to another UITextView?

Use this code and don't pass argument if you don't need
UIToolbar* numberToolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
numberToolbar.barStyle = UIBarStyleBlackTranslucent;
numberToolbar.items = [NSArray arrayWithObjects:[[UIBarButtonItem alloc]initWithTitle:#"Done" style:UIBarButtonItemStyleDone target:self action:#selector(done)],nil];
[numberToolbar sizeToFit];
self.yourTextView.inputAccessoryView = numberToolbar;
-(void)done
{
NSLog(#"hello");
}

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];
}

How do you hide a UIPickerview on button tap programmatically?

i have a uipickerivew in uitablview prototype cell, i want to hide only when done button pressed. please help me.
here is my code for how i create uipickeriview and done button.
self.pickerView = [[UIPickerView alloc] initWithFrame:(CGRect){{0, 0}, 330, 200}];
self.pickerView.delegate = self;
self.pickerView.dataSource = self;
self.pickerView.center = (CGPoint){160, 640};
self.pickerView.hidden = YES;
self.pickerView.backgroundColor =[UIColor whiteColor];
[self.view addSubview:self.pickerView];
UIToolbar *toolBar= [[UIToolbar alloc] initWithFrame:CGRectMake(0,0,320,44)];
[toolBar setBarStyle:UIBarStyleBlackOpaque];
UIBarButtonItem *flex = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
UIBarButtonItem *barButtonDone = [[UIBarButtonItem alloc] initWithTitle:#"Done"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(pickerDoneClicked)];
toolBar.items = #[barButtonDone];
toolBar.items = #[flex, barButtonDone];
barButtonDone.tintColor = [UIColor lightGrayColor];
[_pickerView addSubview:toolBar];
-(void) pickerDoneClicked {
[_pickerView resignFirstResponder];
}
It is better to rather than create and remove each time, create your pickerview once in view did load and then in your cell do:
[_pickerView becomeFirstResponder];
(to show it like a keyboard animated in).
You can then create a pickerDoneClicked method (that is called when Done is tapped) to call resignFirstResponder on your pickerview (to animate it away):
-(void) pickerDoneClicked {
[_pickerView resignFirstResponder];
}
just add below IBAction method and call removefromsuperview method,
-(void)pickerDoneClicked{
[self.pickerView removeFromSuperview];}
As you are already adding again everytime, you can directly remove it from superview and adding again.
After selecting a value using picker view you can implement this method.Add a toolbar with done button and give action to done button
- (IBAction)doneClicked:(id)sender {
[yourTextfield resignFirstResponder];
}
this method is used in the case , when selecting a value to the textfield with picker as input.
- (void)viewDidLoad {
self.pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, 330, 200)];
self.pickerView.delegate = self;
self.pickerView.dataSource = self;
self.pickerView.backgroundColor =[UIColor whiteColor];
[self.view addSubview:self.pickerView];
UIToolbar *toolBar= [[UIToolbar alloc] initWithFrame:CGRectMake(0,0,320,44)];
[toolBar setBarStyle:UIBarStyleBlackOpaque];
UIBarButtonItem *barButtonDone = [[UIBarButtonItem alloc] initWithTitle:#"Done"
style: UIBarButtonItemStylePlain
target:self
action:#selector(pickerDoneClicked:)];
UIBarButtonItem *flex = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
toolBar.items = #[flex, barButtonDone];
barButtonDone.tintColor = [UIColor lightGrayColor];
[self.view addSubview:toolBar];
}
-(IBAction)pickerDoneClicked:(id)sender
{
pickerView.hidden = YES;
}
Inside the didiSelectRow method add this line of code:
self.view.endEditing(true)

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.

UIToolbar not showing

I want a UIToolbar to show up when I click on a certain UITextField, but the toolbar, defined in the #interface, doesn't show up.
This is my code for initializing and attaching it to the text field:
toolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
toolbar.barStyle = UIBarStyleBlackTranslucent;
toolbar.items = [NSArray arrayWithObjects:[[UIBarButtonItem alloc]initWithTitle:#"Done" style:UIBarButtonItemStyleBordered target:self action:#selector(isDone:)], nil];
toolbar.hidden = NO;
[toolbar sizeToFit];
angleField.inputAccessoryView = toolbar;
Can someone see what I'm doing wrong?
P.S. I had used similar-looking code in another project, and it worked.
Here is the code:
UIToolbar* numberToolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
numberToolbar.barStyle = UIBarStyleBlackTranslucent;
numberToolbar.items = [NSArray arrayWithObjects:
[[UIBarButtonItem alloc]initWithTitle:#"Clear" style:UIBarButtonItemStyleBordered target:self action:#selector(cancelNumberPad:)],
[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
[[UIBarButtonItem alloc]initWithTitle:#"Done" style:UIBarButtonItemStyleDone target:self action:#selector(doneWithNumberPad:)],
nil];
[numberToolbar sizeToFit];
changes.inputAccessoryView = numberToolbar;
where changes is a UITextField
I'm not sure if this is it, but what I did was instead of calling setItems: on the toolbar, I called setItems:animated: and now it works...
Thank you to all who responded and took the time to try to help me.
Happy coding!

Resources