0
votes

I'm trying to put a UIButton inside UITextfield, to act as a "Done" button to resign the responder. I've tried with the following code, when the field starts editing the done button in the UITextField leftView is visible, however if I select a value from the datePicker or make any text input with the keyboard (in simulator), the done button disappears and the right clearButton is shown.

It seems like it toggles between them both, I changed textFld.LeftViewMode to display always the button but that is not what I'd like...

Thanks in advance!.

- (void)viewDidLoad
{
    [super viewDidLoad];


    txtFld = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 310, 25)];
    txtFld.placeholder = @"__/__/____";
    txtFld.font = [UIFont fontWithName:@"HelveticaNeue" size:11];
    txtFld.textAlignment = NSTextAlignmentCenter;
    txtFld.contentVerticalAlignment = UIControlContentHorizontalAlignmentCenter;
    txtFld.contentHorizontalAlignment = UIControlContentVerticalAlignmentCenter;
    txtFld.clearButtonMode = UITextFieldViewModeWhileEditing;
    txtFld.borderStyle = UITextBorderStyleRoundedRect;

    UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    doneButton.bounds = CGRectMake(0, 0, 40, 20);
    doneButton.frame = CGRectMake(0.0, 0.0, 40.0, 20.0);
    doneButton.titleLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:10];
    [doneButton setTitle:@"Done" forState:UIControlStateNormal];
    doneButton.imageEdgeInsets = UIEdgeInsetsMake(0, -16, 0, 0);
    [doneButton addTarget:self action:@selector(resignResponder:) forControlEvents:UIControlEventTouchUpInside];
    doneButton.userInteractionEnabled = YES;

    // Add button to the UITextField
    txtFld.leftView = doneButton;
    txtFld.leftViewMode = UITextFieldViewModeWhileEditing;

    UIDatePicker *datePicker = [[UIDatePicker alloc] init];
    datePicker.datePickerMode = UIDatePickerModeDate;
    [datePicker addTarget:self action:@selector(datePickerValueChanged:) forControlEvents:UIControlEventValueChanged];
    [txtFld setInputView:datePicker];

    [self.view addSubview:txtFld];

}

-(void)resignResponder:(UIButton *)sender{
        UITextField *associatedTxtFld = (UITextField *) sender.superview ;
        if ([associatedTxtFld isFirstResponder]) {
            [associatedTxtFld resignFirstResponder];
        }
}

-(void)datePickerValueChanged:(id)sender{
    UIDatePicker *picker = (UIDatePicker *)sender;
    NSDateFormatter *formater = [[NSDateFormatter alloc]init];
    [formater setDateFormat:@"dd-MM-yyyy"];
    txtFld.text = [formater stringFromDate:picker.date];

}
1
You would be better off adding a toolbar as the text field's inputAccessoryView. Add the Done button (and maybe next and previous buttons) to the toolbar. This is a much more common approach. - rmaddy

1 Answers

0
votes

The below code works fine for me . This could be a solution to resign your pickerview

UITextField *dispayNameField = [UITextField  alloc]init];
UIButton *userStatusButton = [UIButton buttonWithType:UIButtonTypeCustom];
    userStatusButton.frame=CGRectMake(0, 0, 20, 20);
    [userStatusButton addTarget:self action:@selector(selectUserStatus) forControlEvents:UIControlEventTouchUpInside];
    displayNameField.rightViewMode = UITextFieldViewModeAlways;
    displayNameField.rightView = _userStatusButton;


-(void)selectUserStatus
{
    displayNameField.inputView = _dataPickerView;
    self.editUserProfileScrollView.scrollEnabled = YES;
    UIActionSheet *actionSheetForDate = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
    [actionSheetForDate showInView:self.parentViewController.tabBarController.view];
    [actionSheetForDate setBounds:CGRectMake(0,0,320, 500)];
    UIDatePicker = [[UIDatePicker alloc]initWithFrame:CGRectMake(0, 50, 320, 216)];
    _dataPickerView.tag = 1;
    _dataPickerView.showsSelectionIndicator = YES;
    _dataPickerView.dataSource = self;
    _dataPickerView.delegate = self;
    [_actionSheetForDate addSubview:_dataPickerView];
    [_actionSheetForDate sendSubviewToBack:_dataPickerView]; 
    UIToolbar *pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
    pickerToolbar.barStyle = UIBarStyleBlackTranslucent;
    [pickerToolbar sizeToFit];
    UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(dismissActionSheetUserField)];
    UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
    UIBarButtonItem *btnBarCancel = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(dismissActionSheetUserField)];
    [pickerToolbar setItems:[NSArray arrayWithObjects:btnBarCancel,flexSpace,doneButton, nil] animated:YES];
    [_actionSheetForDate addSubview:pickerToolbar];
    displayNameField.inputAccessoryView = pickerToolbar;
}