NSNotification 用户信息在按下按钮时保持堆叠
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)
如何只获取最后一个索引?我不需要一堆其他的。
不要在viewdidappear
中使用它!这是一种在 ViewController 中被多次调用的方法。而是在 viewDidLoad
中使用它,它只执行 一次 ,即加载视图控制器时。
你必须在
中调用它
func didReceiveNotification(notification: NSNotification)
函数使用后:
deinit {
NSNotificationCenter.defaultCenter().removeObserver(self)
}
词典实际上是在堆叠还是只是控制台 windows 输出?尝试在每次按下菜单按钮后清除控制台 window 以查看发生了什么。索引的范围是该函数,因此一旦该函数完成它就会消失。
如果您只想要最后点击的索引,只需在您的 class 中声明一个变量并在该变量上设置索引,这样每次检索时您都会得到最后点击的索引
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)")
}
自己找到了正确答案。显然,我所要做的就是将 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)
}
所以我有两个视图控制器,它们同时显示。目标如下:当我按下菜单时,它会返回一个索引。这将通知其他屏幕它需要更新。
我正在做以下事情:
控制器 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)
如何只获取最后一个索引?我不需要一堆其他的。
不要在viewdidappear
中使用它!这是一种在 ViewController 中被多次调用的方法。而是在 viewDidLoad
中使用它,它只执行 一次 ,即加载视图控制器时。
你必须在
func didReceiveNotification(notification: NSNotification)
函数使用后:
deinit {
NSNotificationCenter.defaultCenter().removeObserver(self)
}
词典实际上是在堆叠还是只是控制台 windows 输出?尝试在每次按下菜单按钮后清除控制台 window 以查看发生了什么。索引的范围是该函数,因此一旦该函数完成它就会消失。
如果您只想要最后点击的索引,只需在您的 class 中声明一个变量并在该变量上设置索引,这样每次检索时您都会得到最后点击的索引
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)")
}
自己找到了正确答案。显然,我所要做的就是将 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)
}