誰かが私にメッセージを送って、私が特定のView Controller(ConversationViewController)にいる場合、私は通知を出そうとしています。今、私は通知を提示することができますが、ConversationViewControllerの変数(otherProfileName)にアクセスしようとすると、nilになります。私はそれが変数(otherProfileName)が別のビューコントローラから渡されるためだと思います。変数が正常に渡されたと確信しています。通知は表示され、 "hi"が出力されますが、変数はnilですので、すべてうまく動作します。それを修正するための任意の提案?別のView Controllerから変数にアクセスします。 Swift
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
userInfo ["(other)"] == otherProfileNameのようなものがあれば;そうでなければ一致しない – Tony
あなたは私の答えのポイントを完全に逃しました。トップビューコントローラが 'ConversationViewController'の場合は、値を持たない新しい空の' ConversationViewController'を作成し、その新たに作成された空のView Controllerから値を抽出しようとします。 –