我使用核心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