0

我有 2 个并排的文本字段,countryCodeTextField 和 cellphoneTextField

在国家代码文本字段上。我有一个动作 selectCountry 发生在Edit Did BegincountryCodeTextField


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

  • 请注意, self 实现了<UITextFieldDelegate>.

问题是当用户点击手机时,如果他点击 countryCodeTextField 键盘会显示,键盘永远不会被关闭。

如果此人首先单击国家代码,则键盘永远不会出现(这就是我想要的)。

为什么用户先点击cellphoneTextField再点击countryCodeTextField时键盘不隐藏?

4

3 回答 3

1

如果您不希望用户能够编辑特定的 UITextField,请将其设置为不启用。

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

或者只需在 Interface Builder 中选中已启用的复选框。然后文本字段仍然存在,您将能够通过配置文本来更新它。但正如评论中提到的那样,用户希望 UITextFields 是可编辑的。

另外,为什么要在 IBAction 回调中设置委托?我认为您最好在 Interface Builder 中或在代码中创建 UITextField 时执行此操作。

编辑:

好的 - 所以您希望用户能够选择该框,然后调出一个自定义子视图,他们从中选择将填充该框的内容。

因此,在创建 UITextField 委托时设置它(如上所述)并从 UITextFieldDelegate 协议实现以下内容:

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

返回 NO。请注意,如果您对两个 UITextField 使用相同的委托,则需要使此方法为另一个字段返回 YES。例如,类似:

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

希望这应该停止显示键盘 - 现在你必须弄清楚如何触发你自己的子视图,我建议通过 IBAction 来做(可能是修饰或其他东西)。你必须在这里测试各种东西,但请记住你有点破坏了 UITextField 的点,也许它会工作,也许它不会,也许它会在下一次 iOS 升级中中断。

于 2012-07-11T23:22:45.680 回答
0

在您的方法中:

- (IBAction)textFieldDidBeginEditing: (UITextField *)textField

称之为:[textField becomeFirstResponder];

并对这两个字段应用检查,即当 textField 是 countryCodeTextField 时:

[textField resignFirstResponder];

并调用你的方法:

[self selectCountry];

在此方法中显示国家代码列表。

所以你的代码将是:

 - (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.
}
于 2012-07-12T04:27:20.897 回答
0

好的,首先,我认为您不应该使用 UITextField。我认为您应该使用 UIButton 并将当前值显示为按钮的标题。但是,如果您有心,我会使用我们的好朋友inputView,一个属性 on UITextField,并将其设置为您的自定义输入视图(我假设它是 UIPickerView 或类似的。)

这有一个额外的好处,即不会为盲人和视障用户严重破坏您的应用程序,在您搞乱标准行为之前,您可能应该意识到这一点。

于 2012-07-12T04:08:55.797 回答