如果有人向我发送消息并且我在特定的视图控制器(ConversationViewController)中,我正在尝试提示通知。现在,我可以提供通知,但是当我尝试访问ConversationViewController中的变量(otherProfileName)时,它是nil。我想这是因为该变量(otherProfileName)是从另一个视图控制器传递的。我确信该变量已成功传递。一切都运作良好,因为通知可以显示和"嗨"打印,但变量为零。有任何修复建议吗?
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
}
答案 0 :(得分:0)
这段代码毫无意义:
if topViewController() is ConversationViewController {
let myCustomViewController: ConversationViewController = ConversationViewController(nibName: nil, bundle: nil)
翻译:
“如果顶层视图控制器是ConversationViewController,请创建一个新的空的ConversationViewController实例,并询问该实例是否为otherTrofileName值”
这就像透过蓝盒子的一组盒子一样,因为你知道它包含一个苹果。当你找到一个蓝色的盒子时,去建一个新的空蓝盒子,打开它,并想知道为什么它不包含你的苹果。
答案 1 :(得分:0)
您正在此处
创建ConversationViewController的新实例let myCustomViewController: ConversationViewController = ConversationViewController(nibName: nil, bundle: nil)
尝试
if let myCustomViewController = topViewController() as? ConversationViewController {
print(myCustomViewController.otherProfileName)
print("HI")
HDNotificationView.showNotificationViewWithImage(nil, title: "HI", message: "HHIHI", isAutoHide: true)
}
}