我正在尝试创建我的第一个 MVC 项目,它将检查代码短语并向用户开放访问权限。我创建了 3 个文件:
import Foundation
class passwordCheck {
public static let model = passwordCheck()
func passwordChecking (_ word: String) -> String {
if word == "leohl" {
return "Hello"
} else {
return "Error"
}
}
}
import UIKit
class View: UIView {
public let helloLabel: UILabel = {
let label = UILabel()
label.textAlignment = .center
label.textColor = .black
label.text = "Password?"
label.font = .systemFont(ofSize: 20, weight: .bold)
label.frame = CGRect(x: UIScreen.main.bounds.width/2-75, y: UIScreen.main.bounds.height/2-300, width: 150, height: 30)
return label
}()
public let startButton: UIButton = {
let button = UIButton()
button.setTitle("Start", for: .normal)
button.backgroundColor = .red
button.frame = CGRect(x: UIScreen.main.bounds.width/2-50, y: UIScreen.main.bounds.height/2-50, width: 100, height: 50)
button.layer.cornerRadius = 5
button.setTitleColor(.orange, for: .highlighted)
return button
}()
override init(frame: CGRect) {
super.init(frame: frame)
backgroundColor = .white
addSubview(helloLabel)
addSubview(startButton)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func layoutSubviews() {
super.layoutSubviews()
}
}
import UIKit
class ViewController: UIViewController {
let myView = View()
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
let safeArea = view.safeAreaLayoutGuide.layoutFrame
let theView = View(frame: safeArea)
theView.startButton.addTarget(self, action: #selector(alertCheck), for: .touchUpInside)
view.addSubview(theView)
}
@objc func alertCheck() {
callAlert(alertTitle: "Identify yourself", alertMessage: "Type the code phrase")
}
func callAlert(alertTitle: String, alertMessage: String) {
let alert = UIAlertController(title: alertTitle, message: alertMessage, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Done", style: .default, handler: { action in
self.myView.helloLabel.text = passwordCheck.model.passwordChecking(alert.textFields?.first?.text ?? "")
}))
alert.addTextField()
self.present(alert, animated: true)
print("Button working")
}
}
所以问题是:一切似乎都有效,但是当我在警报中输入代码短语时,没有任何变化。 终端对我说:
-[RTIInputSystemClient remoteTextInputSessionWithID:performInputOperation:] 执行输入 操作需要有效的sessionID
我认为主要问题出在我的模型本身,所以我改变了它:合并
View
和 ViewController
文件,所以我只有两个:ViewController
和 Model
。
视图控制器:
import UIKit
class ViewController: UIViewController {
let myView = View()
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
let safeArea = view.safeAreaLayoutGuide.layoutFrame
let theView = View(frame: safeArea)
theView.startButton.addTarget(self, action: #selector(alertCheck), for: .touchUpInside)
view.addSubview(theView)
}
@objc func alertCheck() {
callAlert(alertTitle: "Identify yourself", alertMessage: "Type the code phrase")
}
func callAlert(alertTitle: String, alertMessage: String) {
let alert = UIAlertController(title: alertTitle, message: alertMessage, preferredStyle: .alert)
alert.addTextField()
alert.addAction(UIAlertAction(title: "Done", style: .default, handler: { action in
self.myView.helloLabel.text = passwordCheck.model.passwordChecking(alert.textFields?.first?.text ?? "")
}))
self.present(alert, animated: true)
print("Button working")
}
}
型号:
import Foundation
class Model {
public static let model = Model()
func passCheck (_ word: String) -> String {
if word == "leohl" {
return "Hello"
} else {
return "Error"
}
}
}
现在一切都按预期进行。但如果您有任何补充,我很想听听!