UIDatePicker with UIToolbar error - ios

I am using UIDatePicker and implement UIToolBar when click on UITextField. But When I click on textfield its crash. I'm using following code.
datepicker = [[UIDatePicker alloc] initWithFrame:CGRectZero];
[datepicker setDatePickerMode:UIDatePickerModeDate];
[datepicker addTarget:self action:#selector(changeDate:) forControlEvents:UIControlEventValueChanged];
UIToolbar *toolbar= [[UIToolbar alloc] initWithFrame:CGRectMake(0,0,self.view.frame.size.width,44)];
toolbar.barStyle = UIBarStyleDefault;
UIBarButtonItem *flexibleSpaceLeft = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem* doneButton = [[UIBarButtonItem alloc] initWithTitle:#"Done" style:UIBarButtonItemStyleDone target:self action:#selector(dismiss)];
[toolbar setItems:[NSArray arrayWithObjects:flexibleSpaceLeft, doneButton, nil]];
[datepicker addSubview:toolbar];
textField.inputAccessoryView = toolbar;
textField.inputView = datepicker;
Terminating app due to uncaught exception
'NSInternalInconsistencyException', reason: 'Please file a radar on
UIKit with this log if you see this assertion. selectorString =
setTitle:, exercisedImplementations = {
"setTitle:" = (
); }'
Thanks in Advance.

Setting a UITextFields inputAccessoryView to a view that already has a superview will cause a crash.
So, Don't add the toolbar as a subview of the UIDatePicker. Remove
[datepicker addSubview:toolbar];
and just set
textField.inputAccessoryView = toolbar;
and the toolbar will appear above the data picker.

Related

I have two text field, I required to open datepicker and another one Timepicker in IOS

I have two textfield, if I click first one, I need to open Datepicker and when I click second textfield I need to open timepicker. but the code is availble in viewdidload so I dont know how to call it seperately.
I required like when I tap first textbox it should open datapicker, when I tap the second textbox it should open the Timepicker.
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[[self navigationController] setNavigationBarHidden:YES animated:YES];
//[self.navigationItem.rightBarButtonItem.customView setHidden:NO];
dateformater = [[NSDateFormatter alloc]init];
datepictxt.delegate = self;
UITextField *textField = (UITextField*)[self.view viewWithTag:1];
datepicker = [[UIDatePicker alloc]init];
if (textField.tag)
{
datepicker.datePickerMode = UIDatePickerModeDate;
[self.datepictxt setInputView:datepicker];
UIToolbar * toolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
[toolbar setTintColor:[UIColor grayColor]];
UIBarButtonItem *donebtn = [[UIBarButtonItem alloc]initWithTitle:#"Done" style:UIBarButtonItemStyleDone target:self action:#selector(showselectiondate:)];
UIBarButtonItem *space = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[toolbar setItems:[NSArray arrayWithObjects:space,donebtn, nil]];
[self.datepictxt setInputAccessoryView:toolbar];
}
else
{
datepicker.datePickerMode = UIDatePickerModeTime;
[self.Timepicker setInputView:datepicker];
UIToolbar * toolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
[toolbar setTintColor:[UIColor grayColor]];
UIBarButtonItem *donebtn = [[UIBarButtonItem alloc]initWithTitle:#"Done" style:UIBarButtonItemStyleDone target:self action:#selector(showselectiontime:)];
UIBarButtonItem *space = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[toolbar setItems:[NSArray arrayWithObjects:space,donebtn, nil]];
[self.Timepicker setInputAccessoryView:toolbar];
}
}
-(void)showselectiontime:(id)response
{
[dateformater setDateFormat:#"hh:mm a"];
self.Timepicker.text= [NSString stringWithFormat:#"%#",[dateformater stringFromDate:datepicker.date]];
[self.Timepicker resignFirstResponder];
}
-(void)showselectiondate:(id)response
{
[dateformater setDateFormat:#"dd/MM/yyyy"];
self.datepictxt.text= [NSString stringWithFormat:#"%#",[dateformater stringFromDate:datepicker.date]];
[self.datepictxt resignFirstResponder];
}
You should use the textField Delegate
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField;
for selecting pickers to open for textfields
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField;{
if(textField==self.datepictxt){
// show date picker
}
}
Currently i think your if statement:
if (textField.tag)
is always gonna be true so it shows date, or you need to allocate a new datePicker, instead of using same one for both time and date modes.
This can be done in the following way:
Let's say you have two textFields:
#property (nonatomic) UITextField *datePickerTextField;
#property (nonatomic) UITextField *timePickerTextField;
You can add a UIDatePicker as an inputView in the following way:
- (void)setDatePickerAsInputViewForTextField: (UITextField *)textField {
// creating UIDatePicker with UIDatePickerModeDate
UIDatePicker *datepicker = [[UIDatePicker alloc] init];
datepicker.datePickerMode = UIDatePickerModeDate;
// creating toolbar above to dismiss the UIDatePicker after selecting date
UIToolbar *inputAccessoryToolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc]initWithTitle:#"Done" style:UIBarButtonItemStyleDone target:self action:#selector(showSelectedDate:)];
UIBarButtonItem *spaceButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[inputAccessoryToolBar setItems:#[doneButton, spaceButton]];
// Adding UIDatePicker object as an inputView to the UITextField so that it appears in place of Keyboard
[textField setInputView:datepicker];
[textField setInputAccessoryView:inputAccessoryToolBar];
}
-(void)showSelectedDate:(id)picker {
[self.view resignFirstResponder]; // to dismiss UITextField
// remaining code to extract date from picker
}
Similarly,
- (void)setTimePickerAsInputViewForTextField: (UITextField *)textField {
UIDatePicker *timepicker = [[UIDatePicker alloc] init];
timepicker.datePickerMode = UIDatePickerModeTime;
UIToolbar *inputAccessoryToolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc]initWithTitle:#"Done" style:UIBarButtonItemStyleDone target:self action:#selector(showSelectedTime:)];
UIBarButtonItem *spaceButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[inputAccessoryToolBar setItems:#[doneButton, spaceButton]];
[textField setInputView:datepicker];
[textField setInputAccessoryView:inputAccessoryToolBar];
}
-(void)showSelectedTime:(id)picker {
[self.view resignFirstResponder]; // to dismiss UITextField
// remaining code to extract date from picker
}

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?

UIDatePicker with UIToolbar

I'm trying to implement UIToolbar on UIDatepicker to dismiss it when touching "Done" button. But when I tap on the button, the datepicker is scrolling and the action button doesn't work.
Here is my code :
datepicker = [[UIDatePicker alloc] initWithFrame:CGRectZero];
[datepicker setDatePickerMode:UIDatePickerModeDate];
[datepicker addTarget:self action:#selector(changeDate:) forControlEvents:UIControlEventValueChanged];
UIToolbar *toolbar= [[UIToolbar alloc] initWithFrame:CGRectMake(0,0,self.view.frame.size.width,44)];
toolbar.barStyle = UIBarStyleDefault;
UIBarButtonItem *flexibleSpaceLeft = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem* doneButton = [[UIBarButtonItem alloc] initWithTitle:#"Done" style:UIBarButtonItemStyleDone target:self action:#selector(dismiss)];
[toolbar setItems:[NSArray arrayWithObjects:flexibleSpaceLeft, doneButton, nil]];
[datepicker addSubview:toolbar];
My datepicker :
Thank you for your help.
If you use UITextField set
textField.inputAccessoryView = toolbar;
textField.inputView = datepicker;
You have to set inputAccessoryView and InputView to to your UITextField
txtField.inputAccessoryView = toolBar;
txtField.inputView = datePickerView;
remove this line from your code:
[datepicker addSubview:toolbar];
and add dismiss method nd add
txtCurrent.inputAccessoryView = toolbar; in didbeginEditing.
-(void)textFieldDidBeginEditing:(UITextField *)textField {
txtCurrent = textField;
[datePicker setHidden:NO];
txtCurrent.inputAccessoryView = toolbar;
if ([textField.text isEqualToString:#""]) {
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
NSDate *eventDate = [NSDate date];
[dateFormat setDateFormat:#"MM/dd/yyyy"];
NSString *dateString = [dateFormat stringFromDate:eventDate];
textField.text = [NSString stringWithFormat:#"%#",dateString];
}
[textField setInputView:datePicker];
}
-(void)dismiss{
datePicker.hidden = YES;
[self.view endEditing:YES];
}
Try this [self.view addSubview:toolbar];
picker = [[UIDatePicker alloc] init];
picker.autoresizingMask = UIViewAutoresizingFlexibleWidth;
picker.datePickerMode = UIDatePickerModeTime;
[picker addTarget:self action:#selector(dueDateChanged:) forControlEvents:UIControlEventValueChanged];
//CGSize pickerSize = [picker sizeThatFits:CGSizeZero];
picker.frame = CGRectMake(0.0, self.view.frame.size.height - 250, self.view.frame.size.width, 250);
picker.backgroundColor = [UIColor whiteColor];
toolbar= [[UIToolbar alloc] initWithFrame:CGRectMake(0,self.view.frame.size.height - 294,self.view.frame.size.width,44)];
toolbar.barStyle = UIBarStyleDefault;
UIBarButtonItem *flexibleSpaceLeft = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIButton* infoButton = [UIButton buttonWithType: UIButtonTypeInfoLight];
[infoButton addTarget:self action:#selector(dismiss) forControlEvents:UIControlEventTouchDown];
UIBarButtonItem* doneButton =[[UIBarButtonItem alloc]initWithCustomView:infoButton];
[toolbar setItems:[NSArray arrayWithObjects:flexibleSpaceLeft, doneButton, nil]];
[self.view addSubview:toolbar];
[self.view addSubview:picker];

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.

Why is this button appearing in all UITextField selections?

I have a UITableViewCell with a text field that's supposed to be for a DatePicker. I need to make a custom button to save the date. But whenever I select any of the UITableViewCell TextFields, the button appears. Why is this?
I'm just trying to "submit" it to the text field so I can save it.
Thanks
- (void)textFieldDidBeginEditing:(UITextField *)dateFieldText
{
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc]
initWithTitle:#"Save Date"
style:UIBarButtonItemStyleDone
target:self
action:#selector(flipView)];
self.navigationItem.rightBarButtonItem = doneButton;
UIDatePicker *datePicker = [[UIDatePicker alloc] init];
datePicker.datePickerMode = UIDatePickerModeDateAndTime;
[datePicker addTarget:self action:#selector(datePickerValueChanged:) forControlEvents:UIControlEventValueChanged];
[self.dateFieldText setInputView:datePicker];
}
Update
Solved my problem using if else block.
- (void)textFieldDidBeginEditing:(UITextField *)sender
{
sender.delegate = self;
if([sender isEqual:dateFieldText])
{
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc]
initWithTitle:#"Save"
style:UIBarButtonItemStyleDone
target:self
action:#selector(saveDate)];
self.navigationItem.rightBarButtonItem = doneButton;
UIDatePicker *datePicker = [[UIDatePicker alloc] init];
datePicker.datePickerMode = UIDatePickerModeDateAndTime;
[datePicker addTarget:self action:#selector(datePickerValueChanged:) forControlEvents:UIControlEventValueChanged];
[self.dateFieldText setInputView:datePicker];
}
else{
UIBarButtonItem *submitButton = [[UIBarButtonItem alloc]
initWithTitle:#"Done"
style:UIBarButtonItemStyleDone
target:self
action:#selector(submitList)];
self.navigationItem.rightBarButtonItem = submitButton;
}
}
Thanks for the help.
Are you reusing table view cells? You might consider making a new cell for your date field and giving it a different reuse identifier from your other cells.
You should set the dataPicker to the intended TextField only i.e you should add an if condition to add the datePicker to the required field only, not for all the fields.
- (void)textFieldDidBeginEditing:(UITextField *)sender
{
sender.delegate = self;
if([sender isEqual:dateFieldText])
{
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc]
initWithTitle:#"Save"
style:UIBarButtonItemStyleDone
target:self
action:#selector(saveDate)];
self.navigationItem.rightBarButtonItem = doneButton;
UIDatePicker *datePicker = [[UIDatePicker alloc] init];
datePicker.datePickerMode = UIDatePickerModeDateAndTime;
[datePicker addTarget:self action:#selector(datePickerValueChanged:) forControlEvents:UIControlEventValueChanged];
[self.dateFieldText setInputView:datePicker];
}
else{
UIBarButtonItem *submitButton = [[UIBarButtonItem alloc]
initWithTitle:#"Done"
style:UIBarButtonItemStyleDone
target:self
action:#selector(submitList)];
self.navigationItem.rightBarButtonItem = submitButton;
}
}

Resources