NSNotificationCenter不在Swift中发送消息

时间:2022-09-10 19:18:06

I use Core Spotlight, and I use UIPageController as RootViewController and several UIViewControllers. Every controller has UIView I drawing a circle of information in there. When I slide between controllers and open new controller I sent NSNotification from current UIViewController to current UIView which is located on current UIViewController. I sent NSNotification that so turn on NSTimer in UIView for updating of information. It works is fine when I just open the app. My problem is when I find infromation from the app in spotlight search and open needed controller from spotlight search NSNotification doesn't sent. I tried several different methods and I cannot to find how I can to make it. Please tell me how can I make it?

我使用Core Spotlight,我使用UIPageController作为RootViewController和几个UIViewControllers。每个控制器都有UIView我在那里画了一圈信息。当我在控制器之间滑动并打开新控制器时,我将NSNotification从当前的UIViewController发送到当前的UIView,它位于当前的UIViewController上。我发送了NSNotification,因此在UIView中打开NSTimer来更新信息。当我打开应用程序时它工作正常。我的问题是当我在聚光灯搜索中找到来自应用程序的信息并从聚光灯搜索打开所需的控制器时NSNotification没有发送。我尝试了几种不同的方法,我无法找到如何制作它。请告诉我我该怎么做?

UPDATE I also tried singleton pattern for NSNotificationCenter but it also didn't help me.

更新我也试过NSNotificationCenter的单例模式,但它也没有帮助我。

UPDATE I tried write Selector as notificationCenter.addObserver(self, selector: Selector(turnOnMemoryTimer()), name: "turnOnMemoryArc", object: nil) and it works when I launch app from spotlight search but it doesn't work when I just launch the app. For any help on this matter thanks!

更新我尝试编写Selector作为notificationCenter.addObserver(self,selector:Selector(turnOnMemoryTimer()),name:“turnOnMemoryArc”,object:nil)当我从聚光灯搜索启动应用程序时它起作用但是当我刚才它不起作用启动应用程序。对此事的任何帮助谢谢!

I also tried to send notification from AppDelegate but it also didn't work

我也尝试从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)
    }

2 个解决方案

#1


1  

I had a similiar issue and solved it like this:

我有一个类似的问题并解决了这个问题:

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

Receive AnyObject:

func turnOnMemoryTimer(notification: AnyObject) {

}

This needed change happened "spontaneous" the normal code was working for months.

这需要的变化发生在“自发的”正常代码工作了几个月。

#2


0  

I think you need to add the NSNotification as parameter to the function you're calling. So, two tweeks:

我认为您需要将NSNotification作为参数添加到您正在调用的函数中。所以,两个tweeks:

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

and

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

#1


1  

I had a similiar issue and solved it like this:

我有一个类似的问题并解决了这个问题:

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

Receive AnyObject:

func turnOnMemoryTimer(notification: AnyObject) {

}

This needed change happened "spontaneous" the normal code was working for months.

这需要的变化发生在“自发的”正常代码工作了几个月。

#2


0  

I think you need to add the NSNotification as parameter to the function you're calling. So, two tweeks:

我认为您需要将NSNotification作为参数添加到您正在调用的函数中。所以,两个tweeks:

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

and

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