【问题标题】:UIButton inside UITextFieldUITextField 内的 UIButton
【发布时间】:2013-03-18 18:23:59
【问题描述】:

我正在尝试在 UITextfield 中放置一个 UIButton,以充当“完成”按钮来让响应者辞职。我已经尝试使用以下代码,当字段开始编辑 UITextField leftView 中的完成按钮时可见,但是如果我从 datePicker 中选择一个值或使用键盘(在模拟器中)进行任何文本输入,完成按钮就会消失并显示正确的 clearButton。

似乎它在两者之间切换,我将 textFld.LeftViewMode 更改为始终显示按钮,但这不是我想要的......

提前致谢!

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

}

【问题讨论】:

  • 您最好添加一个工具栏作为文本字段的 inputAccessoryView。将完成按钮(可能还有下一个和上一个按钮)添加到工具栏。这是一种更常见的方法。

标签: ios uitextfield


【解决方案1】:

下面的代码对我来说很好用。这可能是辞职您的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;
}

【讨论】:

  • 谢谢! rmaddy,Vinodh,我可能会改变主意并采用您建议的方法。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-28
  • 1970-01-01
  • 1970-01-01
  • 2017-10-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多