我正在创建一个增长/缩小的今天扩展以节省空间。在通知中心,但我遇到了NSNotificationCenter的问题。如果我调用visibility()函数,视图会缩小并正常增长,但如果我尝试发布通知,则扩展会失败并尝试重新加载(至少第一次,第二次扩展只是说"无法加载"。为什么会这样?
var NSNotificationDidChoose = "NSNotificationDidChoose"
@IBOutlet var tableView: UITableView!
@IBOutlet var activityIndicator: UIActivityIndicatorView!
@IBAction func shrink(sender: AnyObject) {
//visibility(["bool":false])works fine here
NSNotificationCenter.defaultCenter().postNotificationName(NSNotificationDidChoose, object: nil, userInfo: ["bool":false])
//Crashes and the extension reloads
}
@IBAction func unshrink(sender: AnyObject) {
//visibility(["bool":true]) works fine here
NSNotificationCenter.defaultCenter().postNotificationName(NSNotificationDidChoose, object: nil, userInfo: ["bool":true])
//Crashes and the extension reloads
}
@IBOutlet var buttonview: UIView!
func visibility(boole:[NSObject:AnyObject]) {
var bool = boole["bool"] as Bool
println(bool)
tableView.hidden = !bool
activityIndicator.hidden = !bool
if bool {
self.preferredContentSize = CGSize(width: 350, height: 420)
} else {
self.preferredContentSize = CGSize(width: 350, height: buttonview.frame.height+25)
}
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(true)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "visibility:", name: NSNotificationDidChoose, object: nil)
}
答案 0 :(得分:0)
通知方法的参数是NSNotification
。试试这个。
func visibility(notif: NSNotification) {
let boole = notif.userInfo!
var bool = boole["bool"] as Bool
....
}