按下按钮时,NSNotification userinfo保持堆叠

时间:2016-06-16 12:55:46

标签: ios swift nsnotificationcenter nsnotifications nsnotification

所以我有两个viewcontrollers,它们同时显示。目标如下:当我按下菜单时,它会返回一个索引。这将通知另一个屏幕需要更新。

我正在做以下事情:

控制器A(菜单)

   func carbonTabSwipeNavigation(carbonTabSwipeNavigation: CarbonTabSwipeNavigation, didMoveAtIndex index: UInt) {
    //NSLog("Did move at index: %ld", index)
        //NSNotification to send data
        NSNotificationCenter.defaultCenter().postNotificationName(NotificationNames.GetIndexCarbonKit, object: nil, userInfo: ["clickedIndex" : Int(index)])
}

控制器B(接收器)

override func viewWillAppear(animated: Bool) {
    NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(SearchResults.didReceiveNotification(_:)), name: NotificationNames.GetIndexCarbonKit, object: nil)
}


func didReceiveNotification(notification: NSNotification) {
    let index:Dictionary<String,Int> = notification.userInfo as! Dictionary<String,Int>
    let messageFromNotification = index["clickedIndex"]
    print(" SearchResults now shows index: \(messageFromNotification)")
}

我的问题如下:我将索引从A发送到B的字典,保持堆叠。因此,如果我多次按菜单,我的输出如下:

 SearchResults now shows index: Optional(0)
 SearchResults now shows index: Optional(1)
 SearchResults now shows index: Optional(1)
 SearchResults now shows index: Optional(2)
 SearchResults now shows index: Optional(2)
 SearchResults now shows index: Optional(2)
 SearchResults now shows index: Optional(3)
 SearchResults now shows index: Optional(3)
 SearchResults now shows index: Optional(3)
 SearchResults now shows index: Optional(3)
 SearchResults now shows index: Optional(1)
 SearchResults now shows index: Optional(1)
 SearchResults now shows index: Optional(1)
 SearchResults now shows index: Optional(1)
 SearchResults now shows index: Optional(1)

我怎样才能获得最后一个索引?我不需要堆叠其他人。

4 个答案:

答案 0 :(得分:1)

我自己找到了正确的答案。显然我只需要将NotificationCenter添加到ViewWillAppear,然后在ViewWillDisappear中将其删除。当我在视图之间切换菜单(相同视图的4倍)时,它只返回一个值。

override func viewWillAppear(animated: Bool) {
    NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(SearchResults.didReceiveNotification(_:)), name: NotificationNames.GetIndexCarbonKit, object: nil)
}

override func viewWillDisappear(animated: Bool) {
    NSNotificationCenter.defaultCenter().removeObserver(self)
}

func didReceiveNotification(notification: NSNotification) {
    let index:Dictionary<String,Int> = notification.userInfo as! Dictionary<String,Int>
    self.currentIndex = (index.first?.1)!
    print(currentIndex)
}

答案 1 :(得分:0)

请勿在{{1​​}}中使用它!这是一种在ViewController中被多次调用的方法。而是在viewdidappear中使用它,只执行一次,即视图控制器加载时。  你必须在

中调用它
viewDidLoad

使用功能后:

func didReceiveNotification(notification: NSNotification)

答案 2 :(得分:0)

字典实际上是堆叠还是只是控制台窗口输出?每次按下菜单按钮后,请尝试清除控制台窗口,以查看最新情况。索引的作用域是该函数,因此一旦该函数完成,它就会消失。

答案 3 :(得分:0)

如果您只想要最后一次单击的索引,只需在类中声明一个变量并在该变量上设置索引,这样每次检索时都会得到最后一次单击的索引

func didReceiveNotification(notification: NSNotification) {
    let index:Dictionary<String,Int> = notification.userInfo as! Dictionary<String,Int>
    self.messageFromNotification = index["clickedIndex"]
    print(" SearchResults now shows index: \(messageFromNotification)")
}