【发布时间】:2015-02-23 08:08:23
【问题描述】:
我第一次尝试在标签栏控制器之间传递数据,但它不起作用。 第一控制器:
class FirstViewController: UIViewController {
@IBOutlet weak var sentNotificationLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
NSNotificationCenter.defaultCenter().addObserver(self, selector: "updateNotificationSentLabel:", name: mySpecialNotificationKey, object: nil)
}
@IBAction func notify() {
NSNotificationCenter.defaultCenter().postNotificationName(mySpecialNotificationKey, object: nil, userInfo:["message":"Something"])
}
func updateNotificationSentLabel(notification:NSNotification) {
let userInfo:Dictionary<String,String!> = notification.userInfo as Dictionary<String,String!>
let messageString = userInfo["message"]
sentNotificationLabel.text = messageString
}
}
这里没问题,sentNotificationLabel.text 是“Something”
第二个控制者类似,但他没有收到任何通知。
class SecondViewController: UIViewController {
@IBOutlet weak var notificationLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
NSNotificationCenter.defaultCenter().addObserver(self, selector: "updateLabel:", name: mySpecialNotificationKey, object: nil)
}
func updateLabel(notification:NSNotification) {
let userInfo:Dictionary<String,String!> = notification.userInfo as Dictionary<String,String!>
let messageString = userInfo["message"]
notificationLabel.text = messageString
}
}
我做错了什么?如何改变它?
【问题讨论】:
-
您能否描述一下您是如何检查通知未通过的?请记住,在用户选择之前,不会加载第二个选项卡中显示的视图控制器。
-
好的,你是对的。当我第一次选择第二个标签栏并返回到第一个控制器时,它就可以工作了。我可以在此之前加载它吗?
标签: ios swift uitabbarcontroller nsnotificationcenter message-passing