2016-04-30 14 views
0

誰かが私にメッセージを送って、私が特定の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 
} 

答えて

0

は、このコードは意味をなさない:

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

翻訳:

「トップビューコントローラがコンの場合versationViewController、ConversationViewControllerの新しい空のインスタンスを作成し、そのインスタンスにotherProfileNameの値を尋ねます。

青いボックスには、リンゴが含まれていることがわかっているので、青い箱が見つかったら、新しい空の青い箱を作っ​​て開き、それがなぜあなたのリンゴを含んでいないのか疑問に思う。私は現在、他の(otherProfileName)が私にメッセージを送って誰かに一致するかどうかを確認したいので

+0

を試してみてください。 – Tony

+0

userInfo ["(other)"] == otherProfileNameのようなものがあれば;そうでなければ一致しない – Tony

+0

あなたは私の答えのポイントを完全に逃しました。トップビューコントローラが 'ConversationViewController'の場合は、値を持たない新しい空の' ConversationViewController'を作成し、その新たに作成された空のView Controllerから値を抽出しようとします。 –

0

あなたがここに

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

にConversationViewControllerの新しいインスタンスを作成しているが

 if let myCustomViewController = topViewController() as? ConversationViewController { 
      print(myCustomViewController.otherProfileName) 
      print("HI") 
      HDNotificationView.showNotificationViewWithImage(nil, title: "HI", message: "HHIHI", isAutoHide: true) 
     } 
    } 
関連する問題