我使用核心Spotlight,我使用UIPageController
作为RootViewController
和几个UIViewControllers
。每个控制器都有UIView
我在那里画一圈信息。当我在控制器之间滑动并打开新的控制器时,我将NSNotification
从当前的UIViewController
发送到位于当前UIViewController
上的当前UIView。我发送NSNotification
,因此在UIView
中打开NSTimer
以更新信息。当我刚打开应用程序时,它工作正常。我的问题是,当我从聚光灯搜索中的应用程序中找到信息并从聚光灯搜索中打开所需的控制器NSNotification
未发送。我尝试了几种不同的方法,但我无法找到如何做到这一点。请告诉我我该怎么做?NSNotificationCenter不发送消息Swift
UPDATE我也尝试过单身模式NSNotificationCenter
但它也没有帮助我。
UPDATE我试着写选择为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)
}
您是否检查'notificationCenter'的值不是'nil' – mak
@mak是的,我做了 – Alexander