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