gpt4 book ai didi

ios - 以编程方式从 subview 快速返回按键按下事件

转载 作者:行者123 更新时间:2023-11-28 06:01:09 25 4
gpt4 key购买 nike

我是 swift 的新手,在不使用 Storyboard的情况下创建我的应用程序。我创建了 logincontroller 并将 loginview 附加到它。当有人完成电子邮件和密码文本字段并从键盘按下回车键时,我想检测事件以便处理用户登录请求。

这是我的代码。

import UIKit

class LoginController: UIViewController {

var loginView: LoginView!

override func viewDidLoad() {
super.viewDidLoad()
setupView()
}

func setupView() {
let mainView = LoginView(frame: self.view.frame)
self.loginView = mainView
self.view.addSubview(loginView)
}

和 LoginView.swift 的代码

import UIKit

class LoginView: UIView {

override init(frame: CGRect) {
super.init(frame: frame)
setup()
}

func setup() {

backgroundColor = .white
let stackView = createStackView(views: [emailTextField,passwordTextField])
addSubview(stackView)

//autolayout constraints here

}

let emailTextField: UITextField = {
let tf = UITextField(placeHolder: "Email")
return tf
}()

let passwordTextField: UITextField = {
let tf = UITextField(placeHolder: "Password")
tf.isSecureTextEntry = true
tf.returnKeyType = UIReturnKeyType.send
return tf
}()

最佳答案

只需将此添加到setup 方法

func setup() {
backgroundColor = .white
let stackView = createStackView(views: [emailTextField,passwordTextField])
addSubview(stackView)
emailTextField.delegate = self // This
passwordTextField.delegate = self // And this
}

然后将其添加到您的 LoginView 类下面

extension LoginView: UITextFieldDelegate {
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
if textField == passwordTextField {
passwordTextField.resignFirstResponder()
doWhatYouWantTo() // eg. do your login
} else {
passwordTextField.becomeFirstResponder()
}
return false
}
}

扩展答案-

class LoginController: UIViewController {
var loginView: LoginView!

override func viewDidLoad() {
super.viewDidLoad()
setupView()
}

func setupView() {
let mainView = LoginView(frame: self.view.frame)
self.loginView = mainView
self.loginView.delegate = self
self.view.addSubview(loginView)
}
}

extension LoginController: LoginViewDelegate{
func userWantsToLoginWith(userName: String?, password: String?) {
// Now you have both userName and password here
}
}

protocol LoginViewDelegate: class {
func userWantsToLoginWith(userName: String?, password: String?)
}

class LoginView: UIView {
var delegate: LoginViewDelegate?

let emailTextField: UITextField = {
let tf = UITextField(placeHolder: "Email")
return tf
}()

let passwordTextField: UITextField = {
let tf = UITextField(placeHolder: "Password")
tf.isSecureTextEntry = true
tf.returnKeyType = UIReturnKeyType.send
return tf
}()

override init(frame: CGRect) {
super.init(frame: frame)
setup()
}

func setup() {
backgroundColor = .white
let stackView = createStackView(views: [emailTextField, passwordTextField])
addSubview(stackView)
//autolayout constraints here
}
}

extension LoginView: UITextFieldDelegate {
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
if textField == passwordTextField {
passwordTextField.resignFirstResponder()
delegate?.userWantsToLoginWith(userName: emailTextField.text, password: passwordTextField.text)
} else {
passwordTextField.becomeFirstResponder()
}
return false
}
}

关于ios - 以编程方式从 subview 快速返回按键按下事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49686247/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com