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

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

我有一个pagMenuController on did select方法我正在通过索引。而在另一个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值没有显示。 3,以及它的到来时间。我错过了什么。

有解决方案吗

谢谢

ios swift nsnotificationcenter
1个回答
0
投票

将观察者移动到viewdidload

 override func viewDidLoad() {
    super.viewDidLoad()
        NotificationCenter.default.addObserver(self, selector:  #selector(ContViewController.incomingNotification(_:)), name:  NSNotification.Name(rawValue: "updateView"), object: nil)
 }
© www.soinside.com 2019 - 2024. All rights reserved.