Why is this button appearing in all UITextField selections? - ios

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

Related

UIDatePicker with UIToolbar error

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.

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
}

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)

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.

Resources