首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Swift 3上的NotificationCenter问题

Swift 3上的NotificationCenter问题
EN

Stack Overflow用户
提问于 2016-07-05 21:26:37
回答 3查看 117.6K关注 0票数 92

我正在学习Swift 3,我正在尝试使用NSNotificationCenter。下面是我的代码:

代码语言:javascript
复制
func savePost(){
    let postData = NSKeyedArchiver.archivedData(withRootObject: _loadedpost)
    UserDefaults.standard().object(forKey: KEY_POST)
}
func loadPost(){
    if let postData = UserDefaults.standard().object(forKey: KEY_POST) as? NSData{
        if let postArray = NSKeyedUnarchiver.unarchiveObject(with: postData as Data) as? [Post]{
                _loadedpost = postArray
        }
    }
    //codeerror
    NotificationCenter.default().post(NSNotification(name: "loadedPost" as NSNotification.Name, object: nil) as Notification)
}

这是观察者:

代码语言:javascript
复制
override func viewDidLoad() {
    super.viewDidLoad()
//codeerorr
    NotificationCenter.default().addObserver(self, selector: Selector(("onPostLoaded")), name: "loadedPost", object: nil)
}

func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

它总是给我一个错误“SIGBRT信号”。当我尝试更改观察器中的名称时,这不是一个错误,但显然它没有显示任何内容。我该如何解决这个问题?

EN

回答 3

Stack Overflow用户

发布于 2016-10-10 01:29:23

通知似乎再次发生变化(2016年10月)。

//注册接收通知

代码语言:javascript
复制
NotificationCenter.default.addObserver(self, selector: #selector(yourClass.yourMethod), name: NSNotification.Name(rawValue: "yourNotificatioName"), object: nil)

//发布通知

代码语言:javascript
复制
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "yourNotificationName"), object: nil)
票数 18
EN

Stack Overflow用户

发布于 2016-10-23 03:12:04

对于所有在Swift 3或Swift 4中努力使用#选择器的人,这里有一个完整的代码示例:

代码语言:javascript
复制
// WE NEED A CLASS THAT SHOULD RECEIVE NOTIFICATIONS
    class MyReceivingClass {

    // ---------------------------------------------
    // INIT -> GOOD PLACE FOR REGISTERING
    // ---------------------------------------------
    init() {
        // WE REGISTER FOR SYSTEM NOTIFICATION (APP WILL RESIGN ACTIVE)

        // Register without parameter
        NotificationCenter.default.addObserver(self, selector: #selector(MyReceivingClass.handleNotification), name: .UIApplicationWillResignActive, object: nil)

        // Register WITH parameter
        NotificationCenter.default.addObserver(self, selector: #selector(MyReceivingClass.handle(withNotification:)), name: .UIApplicationWillResignActive, object: nil)
    }

    // ---------------------------------------------
    // DE-INIT -> LAST OPTION FOR RE-REGISTERING
    // ---------------------------------------------
    deinit {
        NotificationCenter.default.removeObserver(self)
    }

    // either "MyReceivingClass" must be a subclass of NSObject OR selector-methods MUST BE signed with '@objc'

    // ---------------------------------------------
    // HANDLE NOTIFICATION WITHOUT PARAMETER
    // ---------------------------------------------
    @objc func handleNotification() {
        print("RECEIVED ANY NOTIFICATION")
    }

    // ---------------------------------------------
    // HANDLE NOTIFICATION WITH PARAMETER
    // ---------------------------------------------
    @objc func handle(withNotification notification : NSNotification) {
        print("RECEIVED SPECIFIC NOTIFICATION: \(notification)")
    }
}

在这个例子中,我们尝试从AppDelegate获取帖子(所以在AppDelegate中实现这个):

代码语言:javascript
复制
// ---------------------------------------------
// WHEN APP IS GOING TO BE INACTIVE
// ---------------------------------------------
func applicationWillResignActive(_ application: UIApplication) {

    print("POSTING")

    // Define identifiyer
    let notificationName = Notification.Name.UIApplicationWillResignActive

    // Post notification
    NotificationCenter.default.post(name: notificationName, object: nil)
}
票数 18
EN

Stack Overflow用户

发布于 2017-01-06 08:50:51

我想它又变了。

为了在Xcode8.2中发布这篇文章。

代码语言:javascript
复制
NotificationCenter.default.post(Notification(name:.UIApplicationWillResignActive)
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38204703

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档