如何以编程方式选择 UITextField 进行编辑

Posted

技术标签:

【中文标题】如何以编程方式选择 UITextField 进行编辑【英文标题】:How to programmatically select a UITextField for editing 【发布时间】:2013-07-31 16:18:42 【问题描述】:

我有一个 UITextField 需要以编程方式选择,因此当用户点击 UIDatePicker 中的日期时,它将被发送到该 UITextField。

这是我的代码,它没有选择 UITextField;我已经尝试了所有可能的 UIControlEvent 组合,但它们都不起作用。还有其他想法吗?

    //  set textField parameters
if([whichTextField isEqualToString:@"oStartTime"]) 
    oStartTime.backgroundColor = [UIColor colorWithRed:252.0/255.0 green:255.0/255.0 blue:197.0/255.0 alpha:1.0];
    [oStartTime sendActionsForControlEvents: UIControlEventTouchUpInside];


else if([whichTextField isEqualToString:@"oFinishTime"]) 
    oFinishTime.backgroundColor = [UIColor colorWithRed:252.0/255.0 green:255.0/255.0 blue:197.0/255.0 alpha:1.0];
    [oFinishTime sendActionsForControlEvents: UIControlEventTouchUpInside];

【问题讨论】:

【参考方案1】:

查看 UIResponder 实例方法becomeFirstResponder。

【讨论】:

【参考方案2】:

yourTextField.becomeFirstResponder()

【讨论】:

以上是关于如何以编程方式选择 UITextField 进行编辑的主要内容,如果未能解决你的问题,请参考以下文章