NSNotificationCenter 未在 Swift 中发送消息

NSNotificationCenter doesn't send message in Swift

我用Core Spotlight,我用UIPageController作为RootViewController和几个UIViewControllers。每个控制器都有UIView我在里面画了一个信息圈。当我在控制器之间滑动并打开新控制器时,我将 NSNotification 从当前 UIViewController 发送到当前 UIViewController 上的当前 UIView。我发送了 NSNotification,所以在 UIView 中打开 NSTimer 以更新信息。当我刚打开应用程序时,它工作正常。我的问题是当我在 Spotlight 搜索中找到来自应用程序的信息并从 Spotlight 搜索打开所需的控制器时 NSNotification 没有发送。我尝试了几种不同的方法,但我找不到如何制作它。请告诉我怎样才能做到?

更新 我也尝试过 NSNotificationCenter 的单例模式,但它也没有帮助我。

UPDATE 我尝试将 Selector 写成 notificationCenter.addObserver(self, selector: Selector(turnOnMemoryTimer()), name: "turnOnMemoryArc", object: nil) ,当我从 Spotlight 搜索启动应用程序时它可以工作,但当我刚启动该应用程序时它不起作用。对于此事的任何帮助,谢谢!

我也试过从 AppDelegate 发送通知,但也没成功

 switch startIndex {
        case 1:
            notificatioCenter.postNotificationName("turnOnCPUTimer", object: nil)
        default: break
        }

UIViewController

override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(true)
    if GlobalTimer.sharedInstance.controllerTimer != nil {
        GlobalTimer.sharedInstance.controllerTimer.invalidate()
        GlobalTimer.sharedInstance.viewTimer.invalidate()
    }
    GlobalTimer.sharedInstance.controllerTimer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: "showMemory", userInfo: nil, repeats: true)
    // notifications

    notificationCenter.postNotificationName("turnOnMemoryArc", object: nil) // this message doesn't sent

    if UIDevice.currentDevice().userInterfaceIdiom == .Pad {
        notificationCenter.postNotificationName("checkMemoryOrientation", object: nil)
        notificationCenter.addObserver(self, selector: "changeMemoryOrientation", name: UIDeviceOrientationDidChangeNotification, object: nil)
    }
}

UIView

// MARK: - var and let
    var arc = Arc()
    let notificationCenter = NSNotificationCenter.defaultCenter()
    var boolForMemoryArc = false


       override func drawRect(rect: CGRect) {
        if boolForMemoryArc == false {
            drawMemoryArc()
            boolForMemoryArc = true
        }
        notificationCenter.addObserver(self, selector: "turnOnMemoryTimer", name: "turnOnMemoryArc", object: nil)
        notificationCenter.addObserver(self, selector: "changedMemoryOrientation", name: "checkMemoryOrientation", object: nil)
    }

    func turnOnMemoryTimer() {
        if GlobalTimer.sharedInstance.viewTimer != nil {
            GlobalTimer.sharedInstance.viewTimer.invalidate()
        }
        GlobalTimer.sharedInstance.viewTimer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: "drawMemoryArc", userInfo: nil, repeats: true)
    }

我认为您需要将 NSNotification 作为参数添加到您正在调用的函数中。所以,两个星期:

notificationCenter.addObserver(self, selector: "turnOnMemoryTimer:", name: "turnOnMemoryArc", object: nil)

func turnOnMemoryTimer(notification: NSNotification) {
    // function body
}

我遇到了类似的问题并这样解决了:

notificationCenter.addObserver(self, selector: "turnOnMemoryTimer:", name: "turnOnMemoryArc", object: nil)

接收任何对象:

func turnOnMemoryTimer(notification: AnyObject) {

}

这个需要的改变发生了"spontaneous"正常代码工作了几个月。