Reputation: 485
So I have two viewcontrollers, which are shown at the same time. The goal is the following: when I press the menu, it gives back an index. This will notify the other screen that it needs to update.
I am doing the following:
Controller A (the menu)
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)])
}
Controller B (the receiver)
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)")
}
My issue is the following: the Dictionary in which I'm sending the index from A to B, keeps stacking. So if I press the menu multiple times, my output is the following:
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)
How can I only get the last index? I don't need a stack of the others.
Upvotes: 0
Views: 73
Reputation: 3878
Don't use it in viewdidappear
! It is a method which is called more than once in a ViewController. Instead use it in viewDidLoad
which is executed only once that is when the view controller is loaded.
And you have to call it in the
func didReceiveNotification(notification: NSNotification)
And after the function use:
deinit {
NSNotificationCenter.defaultCenter().removeObserver(self)
}
Upvotes: 0
Reputation: 485
Found the correct answer myself. Apparently all I had to do was add the NotificationCenter to the ViewWillAppear, and remove it at the ViewWillDisappear. When I switch in the menu between the views (which is 4 times the same view) it only gives back one value.
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)
}
Upvotes: 1
Reputation: 914
If you just want the last clicked index just declare a variable in your class and set the index on that variable so you will get the last clicked index every time you retrieve it
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)")
}
Upvotes: 0
Reputation: 231
Is the dictionary actually stacking or is that just the console windows output? Try clearing the console window after each push of the menu button to see whats going on. The index is scoped to that function so once that function is done it disappears.
Upvotes: 0