Search code examples
iosswiftnsnotificationcenternsnotificationscorespotlight

NSNotificationCenter doesn't send message in Swift


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?

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

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!

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

 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)
    }

Solution

  • 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.