1

I'm creating a today extension that grows/shrinks to save space. in the Notification Center, but I am having problems with the NSNotificationCenter. If I call the visibility() function, the view shrinks and grows normally, but if I try posting a notification, the extension fails and attempts to reload instead(at least the first time, the second time the extension just says "Unable to load". Why is this?

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

1 回答 1

0

通知方法的参数是NSNotification。尝试这个。

func visibility(notif: NSNotification) {
    let boole = notif.userInfo!
    var bool = boole["bool"] as Bool
    ....
}
于 2015-01-26T02:40:13.367 回答