简体   繁体   English

隐藏具有多个textFields的IOS键盘

[英]Hide IOS keyboard with multiple textFields

I have 2 textFields side by side, countryCodeTextField and cellphoneTextField 我有2个并排的textField,countryCodeTextField和phoneTextField

On countryCodeTextField. 在countryCodeTextField上。 I have an action selectCountry that happens on Edit Did Begin on the countryCodeTextField 我有一个操作selectCountry发生在countryCodeTextField的Edit Did Begin


- (IBAction)selectCountry:(id)sender {
    countryCodeTextField.delegate = self;
    [countryCodeTextField resignFirstResponder];

  • Note that self implements the <UITextFieldDelegate> . 请注意,self实现了<UITextFieldDelegate>

Problem is when user click's cellphone the keyboard is displayed if he clicks on countryCodeTextField the keyboard is never dismissed. 问题是当用户单击手机时,如果他单击countryCodeTextField,则显示键盘,而键盘永远不会关闭。

If the person clicks the countryCode first then the keyboard never appears(which is what I want). 如果此人首先单击countryCode,则键盘将永远不会出现(这是我想要的)。

Why isn't the keyboard hidden when the user clicks cellphoneTextField first and then countryCodeTextField? 当用户先单击“ phoneTextField”然后单击“ CountryCodeTextField”时,为什么键盘没有隐藏?

If you don't want the user to be able to edit a particular UITextField, set it to not be enabled. 如果您不希望用户能够编辑特定的UITextField,请将其设置为不启用。

 UITextField *textField = ... // Allocated somehow
 textfield.enabled = NO

Or just check the enabled checkbox in Interface Builder. 或仅在Interface Builder中选中已启用的复选框。 Then the textfield will still be there and you'll be able to update it by configuring the text. 然后,文本字段将仍然存在,您将可以通过配置文本来对其进行更新。 But as sort of mentioned in comments, users expect UITextFields to be editable. 但是正如评论中提到的那样,用户希望UITextFields是可编辑的。

Also, why are you setting the delegate in the IBAction callback? 另外,为什么还要在IBAction回调中设置委托? I would think you'd be better off doing this in Interface Builder or when you create the UITextField in code. 我认为您最好在Interface Builder中或在代码中创建UITextField时执行此操作。

EDIT: 编辑:

Ok - so you want users to be able to select the box, but then bring up a custom subview(s) from which they select something which will fill the box. 好的-因此您希望用户能够选择该框,然后调出一个自定义子视图,从中选择一个可以填充该框的子视图。

So set the UITextField delegate when you create it (as mentioned above) and implement the following from the UITextFieldDelegate protocol: 因此,请在创建UITextField委托时进行设置(如上所述),并通过UITextFieldDelegate协议实现以下内容:

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
     return NO;
}

to return NO. 返回NO Note that if you are using the same delegate for both of your UITextFields, you will need to make this method return YES for the other field. 请注意,如果两个UITextField都使用相同的委托,则需要使此方法为另一个字段返回YES。 For example, something like: 例如,类似:

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
   if (textField == countryTextField)  
       return NO;
   return YES;
}

Hopefully this should stop the keyboard being displayed - and now you have to work out how to fire your own subviews, which I'd suggest doing via an IBAction (touch up or something perhaps). 希望这可以停止显示键盘-现在,您必须弄清楚如何触发自己的子视图,我建议您通过IBAction(可能进行润色或其他操作)进行操作。 You'll have to test various things out here, but remember you're kinda corrupting the point of UITextField and maybe it'll work and maybe it won't, and maybe it'll break in the next iOS upgrade. 您必须在此处测试各种情况,但是请记住,您有点破坏UITextField的观点,也许它会起作用,也许不会,并且可能会在下一次iOS升级中中断。

Okay, so first, I think you shouldn't be using a UITextField. 好的,所以首先,我认为您不应该使用UITextField。 I think you should be using a UIButton and have the current value showing as the button's title. 我认为您应该使用UIButton并将当前值显示为按钮的标题。 However, if you have your heart set on it, I would use our good friend inputView , a property on UITextField , and set that to your custom input view (which I assume is a UIPickerView or similar.) 但是,如果您愿意,我可以使用我们的好朋友inputView ,它是UITextField一个属性,并将其设置为您的自定义输入视图(我假设是UIPickerView或类似的视图)。

This has the added bonus of not breaking your app horribly for blind and visually impaired users, something you should probably be aware of before you go messing about with standard behaviour. 这还有一个额外的好处,就是不会为盲人和视力障碍的用户彻底破坏您的应用程序,在您弄乱标准行为之前,您可能应该意识到这些。

In your method : 用你的方法:

- (IBAction)textFieldDidBeginEditing: (UITextField *)textField

call this : [textField becomeFirstResponder]; 称之为:[textField成为FirstResponder];

and apply checks for the two fields ie, when textField is the countryCodeTextField write : 并对两个字段进行检查,即,当textField是countryCodeTextField时,写:

[textField resignFirstResponder];

and call your method : 并调用您的方法:

[self selectCountry];

In this method display the list of country codes. 在此方法中,显示国家代码列表。

So Your code will be : 因此,您的代码将是:

 - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    return YES;
}
 - (IBAction)textFieldDidBeginEditing: (UITextField *)textField{

    [textField becomeFirstResponder];

    if (textField == countryCodeTextField){

    [textField resignFirstResponder];
    [self selectCountry];
    }
}

-(IBAction)selectCountry{
    //display the list no need to do anything with the textfield.Only set text of TextField as the selected countrycode.
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM