NotificationCenter-视图之间的文本传输

时间:2018-06-21 13:00:20

标签: ios swift

我有此代码:

MainViewControler.swift:

 if (empty($_POST['confirmpass']) || $_POST['confirmpass'] != $_POST['userpass'])

AlertStruct.swift:

override func viewDidLoad() {
    super.viewDidLoad()
    NotificationCenter.default.addObserver(self, selector: #selector(showAlertMessage(notification:)), name: Notification.Name("NotificationAlertMessage"), object: errorMessage.self) 

}


@objc func showAlertMessage(notification: NSNotification) {
        print("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! \(notification.title)")
}

Propererties.swift

struct errorMessage{
    var title: String?
    var description: String?
}

Propierties.swift是containerView MainViewControler中的子项。

我想在MainViewControler中显示功能ShowAlertMessage,其中包含从Propierties发送的数据-> func showError()

该怎么做?我的代码不想编译/正常工作:(

2 个答案:

答案 0 :(得分:1)

您的代码几乎是正确的...只需在方法下面进行更新

@objc func showAlertMessage(notification: NSNotification) {

    let object = notification.object as! errorMessage
     print("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! \(object.title)")

}

答案 1 :(得分:0)

尝试一下,测试

override func viewDidLoad() {
    super.viewDidLoad()

    NotificationCenter.default.addObserver(self, selector: #selector(showAlertMessage(notification:)), name: Notification.Name("NotificationAlertMessage"), object: nil)

    NotificationCenter.default.post(name: Notification.Name("NotificationAlertMessage"), object: nil, userInfo: ["object": ErrorMessage(title: "tytuł", description: "description")])
}

@objc func showAlertMessage(notification: NSNotification) {
    if let errorMessage = notification.userInfo?["object"] as? ErrorMessage {
        print("\(errorMessage.title), \(errorMessage.description)")
    }
}