I created a simple page of my app today. And, now I want to expand it.
我今天创建了一个简单的应用页面。而且,现在我想扩展它。
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var myLabel: UILabel!
@IBOutlet weak var myTextField: UITextField!
@IBOutlet weak var display: UILabel!
@IBAction func myButtonPressed(_ sender: Any) {
display.text = "Hi \(myTextField.text!)! What can I do for you
today?"
}
override func viewDidLoad() {
super.viewDidLoad()
}
}
I want the app to prompt a new page after the user entered their name.
我希望应用在用户输入其名称后提示新页面。
3 个解决方案
#1
0
You can conform to the UITextViewDelegate
and present a viewController in func textViewDidEndEditing(_ textView: UITextView)
like:
你可以符合UITextViewDelegate并在func textViewDidEndEditing(_ textView:UITextView)中呈现一个viewController,如:
extension ViewController: UITextFieldDelegate {
func textViewDidEndEditing(_ textView: UITextView) {
self.present(/*the targeted view controller*/)
}
}
#2
0
You can add target like
你可以添加目标
// In viewDidLoad
//在viewDidLoad中
myTextField.addTarget(self, action: #selector(onNameChange(sender:)), for: .editingChanged)
// on value change
//关于价值变化
@objc func onNameChange(sender:UITextField) {
// Do something
}
#3
0
So far I got your query as:
到目前为止,我的查询为:
You want to go to new ViewController when the user is done with filling his name in the textfield.
当用户完成在文本字段中填写他的名字时,您希望转到新的ViewController。
If I got you right then choose the "GO" (or any thing you wish from options) as the Return Key value inside 'Text Input Traits Section' of Attribute Inspector.
如果我找到你,那么在属性检查器的“文本输入特征部分”中选择“GO”(或任何你想要的选项)作为返回键值。
And now add this code in your view controller class with implementing UITextFieldDelegate:
现在,通过实现UITextFieldDelegate,在视图控制器类中添加此代码:
func textFieldShouldReturn(_ textField: UITextField) -> Bool // called when 'return' key pressed. return false to ignore.
{
print(textField.tag)
if (textField.text?.isEmpty)! {
//show alert that text field is empty
return false
}
/* as per your case we have only one textfield,
So there no need of switch case and you can
directly present your next vc from here without having any button on UI */
return false
}
#1
0
You can conform to the UITextViewDelegate
and present a viewController in func textViewDidEndEditing(_ textView: UITextView)
like:
你可以符合UITextViewDelegate并在func textViewDidEndEditing(_ textView:UITextView)中呈现一个viewController,如:
extension ViewController: UITextFieldDelegate {
func textViewDidEndEditing(_ textView: UITextView) {
self.present(/*the targeted view controller*/)
}
}
#2
0
You can add target like
你可以添加目标
// In viewDidLoad
//在viewDidLoad中
myTextField.addTarget(self, action: #selector(onNameChange(sender:)), for: .editingChanged)
// on value change
//关于价值变化
@objc func onNameChange(sender:UITextField) {
// Do something
}
#3
0
So far I got your query as:
到目前为止,我的查询为:
You want to go to new ViewController when the user is done with filling his name in the textfield.
当用户完成在文本字段中填写他的名字时,您希望转到新的ViewController。
If I got you right then choose the "GO" (or any thing you wish from options) as the Return Key value inside 'Text Input Traits Section' of Attribute Inspector.
如果我找到你,那么在属性检查器的“文本输入特征部分”中选择“GO”(或任何你想要的选项)作为返回键值。
And now add this code in your view controller class with implementing UITextFieldDelegate:
现在,通过实现UITextFieldDelegate,在视图控制器类中添加此代码:
func textFieldShouldReturn(_ textField: UITextField) -> Bool // called when 'return' key pressed. return false to ignore.
{
print(textField.tag)
if (textField.text?.isEmpty)! {
//show alert that text field is empty
return false
}
/* as per your case we have only one textfield,
So there no need of switch case and you can
directly present your next vc from here without having any button on UI */
return false
}