如何在Swift中从另一个视图控制器更改标签的插座?

时间:2018-05-08 09:22:51

标签: ios swift

所以我正在使用SmileLock库。输入正确的密码后,当视图再次加载时,我在更改一些文本和插座时遇到问题。这是您导航到密码条目VC的第一个登录页面。我试过并通过我的代码中的注释明确了我的问题。

import UIKit

class LoginVC: UIViewController {

    //Made this to access this VC from other VCs.
    static var instance = LoginVC()

    //A button which is activated if the password is correct and goes to the next page.
    @IBOutlet weak var enterMainPage: UIButton!

    //A label which I'd want to change its text.
    @IBOutlet weak var statusSection: UILabel!

    //MARK: Property
    let isBlurUI = true

    var loginVCID: String!
    var mainTBCID: String!

    override func viewDidLoad() {
        super.viewDidLoad()

        print("correct 8")

        //The button is inactive in every load.
        enterMainPage.isEnabled = false
        PASSWORD_IS_CORRECT: Bool = false

        //This loads up the password entry page when the login button is pressed.
        loginVCID = isBlurUI ? "BlrPasswordLoginVC" : "PasswordLoginVC"
    }


    @IBAction func enterMainPagePressed(_ sender: UIButton) {

    }

    @IBAction func presentLoginVC(_ sender: AnyObject) {
        PASSWORD_IS_CORRECT = true
        print("correct 10")
        present(loginVCID)

        //This was the only way I was not getting any errors (Unexpectedly found nil).
        self.loadView()
    }


    func present(_ id: String) {
        let mainVC = storyboard?.instantiateViewController(withIdentifier: id)
        mainVC?.modalPresentationStyle = .overCurrentContext
        present(mainVC!, animated: true, completion: nil)

    }

}

这是密码输入页面,我试图直接更改我的标签文本,但结果是"在展开时意外发现nil"错误。我将在下面的代码中指出它:

override func viewDidLoad() {
        super.viewDidLoad()
        //create PasswordUIValidation subclass
        passwordUIValidation = MyPasswordUIValidation(in: passwordStackView)

        passwordUIValidation.success = { [weak self] _ in
            print("*️⃣ success!")
            PASSWORD_IS_CORRECT = true
            if PASSWORD_IS_CORRECT {
                //This is where I got the unexpected unwrapping error.
                LoginVC.instance.statusSection.text = "sample text"
                print("password is correct")
            }

我只需要能够更改标签的文字和其他一些更改。随时随地改变。

1 个答案:

答案 0 :(得分:1)

您可以使用Notificationdelegate

  

通知

extension Notification.Name {    
    static let passwordNotification = Notification.Name(
        rawValue: "password_Notification")
}

class LoginVC: UIViewController {

 override func viewDidLoad() {
        super.viewDidLoad()
     let notificationCenter = NotificationCenter.default
       notificationCenter.addObserver(self,
                               selector: #selector(LoginVC.getPassword),
                               name: .passwordNotification,
                               object: nil)
    }

  @objc func getPassword(_ notification:NSNotification){
        if  let password =  notification.object as? String {
            self.statusSection.text =  password
        }
      }
}
密码控制器中的

删除此LoginVC.instance.statusSection.text = "sample text"

并添加此

 let notificationCenter = NotificationCenter.default
                        notificationCenter.post(name: .passwordNotification, object: "sample text")