【发布时间】:2016-03-31 22:52:39
【问题描述】:
我使用 Core Spotlight,我使用 UIPageController 作为 RootViewController 和几个 UIViewControllers。每个控制器都有UIView 我在里面画了一个信息圈。当我在控制器之间滑动并打开新控制器时,我将 NSNotification 从当前 UIViewController 发送到当前 UIView,它位于当前 UIViewController 上。我发送了NSNotification,所以请打开UIView 中的NSTimer 以更新信息。当我打开应用程序时它工作正常。我的问题是,当我在聚光灯搜索中从应用程序中找到信息并从聚光灯搜索中打开所需的控制器时,NSNotification 没有发送。我尝试了几种不同的方法,但我找不到如何做到这一点。请告诉我怎么做?
更新我也尝试了 NSNotificationCenter 的单例模式,但它也没有帮助我。
更新我尝试将 Selector 写为 notificationCenter.addObserver(self, selector: Selector(turnOnMemoryTimer()), name: "turnOnMemoryArc", object: nil)
,当我从聚光灯搜索启动应用程序时它可以工作,但当我启动应用程序时它不起作用。在此问题上的任何帮助,谢谢!
我也尝试从 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)
}
}
界面视图
// 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
标签: ios swift nsnotificationcenter nsnotifications corespotlight