0

I am trying to present a notification if someone send me a message and I am in a specific view controller (ConversationViewController). Right now, I can present the notification, but when I try to access a variable (otherProfileName) in ConversationViewController, it is nil. I guess it is because that variable (otherProfileName) is passed from another view controller. I am sure that the variable is passed successfully. Everything works well as notification can be showed and "hi" is printed, but the variable is nil. Any suggestion to fix it?

ConversationViewController

// passed from another view controller
var otherProfileName = String()

appDelegate

func application(application: UIApplication,  didReceiveRemoteNotification userInfo: [NSObject : AnyObject],  fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
    if application.applicationState == UIApplicationState.Active {
        print(topViewController())
        if topViewController() is ConversationViewController {
            let myCustomViewController: ConversationViewController = ConversationViewController(nibName: nil, bundle: nil)
            print(myCustomViewController.otherProfileName)
            print("HI")
            HDNotificationView.showNotificationViewWithImage(nil, title: "HI", message: "HHIHI", isAutoHide: true)
        }
    }
    completionHandler(UIBackgroundFetchResult.NewData)
}

func topViewController(base: UIViewController? = UIApplication.sharedApplication().keyWindow?.rootViewController) -> UIViewController? {
    if let MMDrawers = base as? MMDrawerController {
        for MMDrawer in MMDrawers.childViewControllers {
            return topViewController(MMDrawer)
        }
    }
    if let nav = base as? UINavigationController {
        return topViewController(nav.visibleViewController)
    }
    if let tab = base as? UITabBarController {
        if let selected = tab.selectedViewController {
            return topViewController(selected)
        }
    }
    if let presented = base?.presentedViewController {
        return topViewController(presented)
    }
    return base
}
Tony
  • 25
  • 7

2 Answers2

0

This code makes no sense:

    if topViewController() is ConversationViewController {
        let myCustomViewController: ConversationViewController = ConversationViewController(nibName: nil, bundle: nil)

Translation:

"If the top view controller is a ConversationViewController, create a new, empty instance of ConversationViewController, and ask that instance for it's otherProfileName value"

It's like looking through a set of boxes for the blue box, since you know it contains an apple. When you find a blue box, go build a new, empty blue box, open it, and wonder why it doesn't contain your apple.

Duncan C
  • 128,072
  • 22
  • 173
  • 272
  • Because I want to check whether the current other (otherProfileName) matches someone who send me the message. – Tony Apr 30 '16 at 01:02
  • something like if userInfo["(other)"] == otherProfileName then match; otherwise, not match – Tony Apr 30 '16 at 01:02
  • You completely missed the point of my answer. If the top view controller is a `ConversationViewController`, you are creating a brand new, empty, `ConversationViewController` that won't have any values in it, and trying to extract a value from that newly created, empty view controller. – Duncan C Apr 30 '16 at 01:05
  • Analogy #2: I left my wallet in the glove box of my Prius. Go look on the street. If I find a Prius, go buy a NEW Prius that's the same color and wonder why the glove box of the new Prius doesn't contain my wallet. That's what you're doing. – Duncan C Apr 30 '16 at 01:07
  • Oh. I got what you mean. let myCustomViewController: ConversationViewController = ConversationViewController(nibName: nil, bundle: nil). That one refer to the original conversationViewController. I thought it refers to the one I am currently on. Thank you for your help. – Tony Apr 30 '16 at 02:28
0

You are creating new instance of ConversationViewController in here

let myCustomViewController: ConversationViewController = ConversationViewController(nibName: nil, bundle: nil)

Try

      if let myCustomViewController = topViewController() as? ConversationViewController {
            print(myCustomViewController.otherProfileName)
            print("HI")
            HDNotificationView.showNotificationViewWithImage(nil, title: "HI", message: "HHIHI", isAutoHide: true)
        }
     }
Ahmad Ishfaq
  • 914
  • 1
  • 8
  • 10