[英]Swift textfield automatic cursor
Is it possible to put a cursor automatically in the first of textfields, when opening a screen?打开屏幕时,是否可以将光标自动放在第一个文本字段中? For example, when I open a screen with Email and Password, I want to right after that type my email, without pressing a textfield.例如,当我打开一个带有电子邮件和密码的屏幕时,我想在此之后立即输入我的电子邮件,而不是按文本字段。
If you have a static tableView, just take an IBOutlet
from your textFields, and in the method viewWillAppear
you can call to yourTextField.becomeFirstResponder()
如果你有一个静态 tableView,只需从你的 textFields 中获取一个IBOutlet
,然后在方法viewWillAppear
你可以调用yourTextField.becomeFirstResponder()
If you have a dynamic tableView, you have some kind of data source object, you can loop in it, find the index of the first textField, then you have to scroll the tableView to that cell, so it becomes visible, and then you should get a reference to the cell with let cell = tableView.cellForRow(at: IndexPathOfYourCell) as? YourCustomTextCell
如果你有一个动态的tableView,你有某种数据源对象,你可以在其中循环,找到第一个textField的索引,然后你必须将tableView滚动到那个单元格,让它变得可见,然后你应该使用let cell = tableView.cellForRow(at: IndexPathOfYourCell) as? YourCustomTextCell
对单元格的引用let cell = tableView.cellForRow(at: IndexPathOfYourCell) as? YourCustomTextCell
let cell = tableView.cellForRow(at: IndexPathOfYourCell) as? YourCustomTextCell
, and then you can do something like cell.textField.becomeFirstResponder()
let cell = tableView.cellForRow(at: IndexPathOfYourCell) as? YourCustomTextCell
,然后你可以做一些像cell.textField.becomeFirstResponder()
Chose whichever textfield you want to become first responder:选择您想成为第一响应者的任何文本字段:
override func viewDidLoad(){
super.viewDidLoad()
emailTextField.becomeFirstResponder()
}
Create an IBOutlet of TextFiled in ViewController and in viewDidLoad()
method call myTextField.becomeFirstResponder()
在 ViewController 和viewDidLoad()
方法中创建 TextFiled 的IBOutlet调用myTextField.becomeFirstResponder()
override func viewDidLoad() {
super.viewDidLoad()
myTextField.becomeFirstResponder()
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.