簡體   English   中英

按下按鈕時,NSNotification userinfo不斷堆疊

[英]NSNotification userinfo keeps stacking when pushing buttons

因此,我有兩個視圖控制器,它們同時顯示。 目標如下:當我按下菜單時,它會返回一個索引。 這將通知另一個屏幕需要更新。

我正在執行以下操作:

控制器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)

如何只獲取最后一個索引? 我不需要一堆其他的。

我自己找到了正確答案。 顯然,我要做的就是將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)
}

不要在viewdidappear使用它! 這是一個在ViewController中多次調用的方法。 而是在viewDidLoad中使用它,該視圖僅在加載視圖控制器時執行一次
而且您必須在

func didReceiveNotification(notification: NSNotification)

並在函數使用后:

deinit {
NSNotificationCenter.defaultCenter().removeObserver(self)
}

字典實際上是在堆疊還是僅控制台窗口輸出? 每次按菜單按鈕后,嘗試清除控制台窗口以查看發生了什么。 索引的作用范圍是該功能,因此一旦完成該功能,它就會消失。

如果只希望最后單擊的索引,只需在類中聲明一個變量,然后在該變量上設置索引,這樣,每次檢索該索引時,都會獲得最后單擊的索引

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)")
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM