1

我使用核心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) 
    } 
+0

您是否檢查'notificationCenter'的值不是'nil' – mak

+0

@mak是的,我做了 – Alexander

回答

1

我有一個類似的問題,解決它像這樣:

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

接收AnyObject:

func turnOnMemoryTimer(notification: AnyObject) { 

} 

這需要改變發生了「自發」的正常代碼工作幾個月。

+0

你好。謝謝你的回答,但它不能幫助我。我現在試了一下。你怎麼看,因爲它發生了什麼? – Alexander

+0

我認爲它發生在最後一次Xcode更新後,但我不確定。 – Darko

+0

我使用Xcode版本7.2(7C68) – Alexander

0

我認爲你需要添加NSNotification作爲您要調用的函數的參數。因此,二tweeks:

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

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

我試過這個方法,但是我得到了同樣的結果。 – Alexander