无法从控制器更改视图

问题描述 投票:0回答:1

我正在尝试创建我的第一个 MVC 项目,它将检查代码短语并向用户开放访问权限。我创建了 3 个文件:

  1. 模型 - 检查代码短语是否正确并返回用作 UILabel 文本的结果。
import Foundation

class passwordCheck {
    public static let model = passwordCheck()
    
    func passwordChecking (_ word: String) -> String {
        if word == "leohl" {
            return "Hello"
        } else {
            return "Error"
        }
    }
}
  1. View - 负责向用户显示UI。包含用于状态的 UILabel 和用于启动识别过程的 UIButton。
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()
    }
}
  1. ViewController - 当按下 UIButton 时调用 UIAlertController。这会更改视图的背景颜色和 UILabel 文本。
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

ios swift model-view-controller uiview uiviewcontroller
1个回答
0
投票

我认为主要问题出在我的模型本身,所以我改变了它:合并

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"
        }
    }
}

现在一切都按预期进行。但如果您有任何补充,我很想听听!

© www.soinside.com 2019 - 2024. All rights reserved.