通知中心值第一次,第二次未更新

时间:2019-02-26 05:50:40

标签: ios swift nsnotificationcenter

我在通过索引的选择方法上使用了pagMenuController。在另一个vc中,我得到了index的值,并且需要显示带有传入的index值的视图。

我的FirstVC中的代码:

func pageMenuController(_ pageMenuController: PageMenuController, didSelectMenuItem index: Int, direction: PageMenuNavigationDirection) {
    NotificationCenter.default.post(name: NSNotification.Name(rawValue: "updateView"), object: nil, userInfo: ["indexValue": index]) 
}

SecondVC中的代码:

override func viewWillAppear(_ animated: Bool) {
    NotificationCenter.default.addObserver(self, selector:  #selector(ContViewController.incomingNotification(_:)), name:  NSNotification.Name(rawValue: "updateView"), object: nil)
}

@objc func incomingNotification(_ notification: Notification) {
    if let indexVal = notification.userInfo?["indexValue"] as? Int {
        print(indexVal)
    }
}

无论何时出现我的屏幕,我都需要获取index值。但是,当我第一次单击菜单项时,第二次index的值未显示。第三,以及以上的时间到来。我错过了什么。

有解决方案吗?

谢谢

1 个答案:

答案 0 :(得分:0)

移动您的观察者查看加载

 override func viewDidLoad() {
    super.viewDidLoad()
        NotificationCenter.default.addObserver(self, selector:  #selector(ContViewController.incomingNotification(_:)), name:  NSNotification.Name(rawValue: "updateView"), object: nil)
 }