2016-11-05 66 views
0

我一直試圖讓這個工作超過1個月,不用說,它變得可笑。我無法弄清楚我的方法是否錯誤,或者是否有導致問題的獨特設置。我已經在下面的過程中嘗試了幾十個變體,並取得零成功。將視圖控制器添加到堆棧頂部

我在AppDelegate中有一個過程,它調用委託控制器中的一個函數,該控制器獲取用戶數據以準備顯示其配置文件。

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) { 
    notificationsDelegate.userId = thisid 
    notificationsDelegate.getUser() 
} 

在notificationsDelegate中,數據檢索的成功代碼觸發,因此在那裏沒有問題。然後,代碼應該將用戶配置文件視圖添加到堆棧的頂部,以便在應用重新打開時顯示,但不會顯示。正在使用的代碼是

var root = UIApplication.sharedApplication().delegate!.window!?.rootViewController as! UINavigationController 
let storyboard = UIStoryboard(name: "Main", bundle: nil) 
let pvc = storyboard.instantiateViewControllerWithIdentifier("UserProfile") as! UserProfile 
pvc.modalPresentationStyle = UIModalPresentationStyle.FullScreen 
pvc.user = user 
root.visibleViewController!.presentViewController(pvc, animated: true, completion: nil) 

我有這個進程中的一個警告,所以我知道它運行,但應用程序只是重新打開無論它是當不被添加在上面用戶控制器關閉。

這個過程看起來是否正確?

順便說一句,我已經試過

root.presentViewController.... 
root.pushViewController.... 
root.topViewController.presentViewController.... 
root.topViewController.presentedViewController.presentViewController... 

添加在呈現一個完成處理程序的顯示,這是沒有得到完成。

+0

也許嘗試將一個鍵值對添加到NSUserDefaults,然後從viewDidLoad將正確的視圖置於頂部,每當應用程序重新打開? –

+0

@ lior感謝您的建議 - 我嘗試過這樣的方法,而且還有問題,可能更容易排除故障 - 主要問題是,我不在乎或不知道什麼視圖是在堆棧頂部時,應用程序重新打開,因爲它應該沒關係,但也許我必須解開用戶沒有任何理由只是爲了確保我在視圖上可以運行提示代碼 – RobertyBob

+0

現在嘗試root.pushViewController ...並沒有好的 – RobertyBob

回答

0

所以我終於研究出了這一點.....

似乎有一些問題與根控制器 - 我不知道是什麼 - 但這似乎只是拒絕的演示新視圖不會崩潰應用程序。

所以首先我需要找到實際的頂層控制器,同時仍然在AppDelegate中,並將其傳遞給notificationsDelegate。

我將下列內容添加到notificationsDelegate。

var topview: AnyObject? 

我在didReceiveRemoteNotification中添加了以下內容。

notificationsDelegate.topview = self.topViewController() 

然後找到頂部控制器使用以下內容。

func topViewController() -> UIViewController? { 
    if UIApplication.sharedApplication().keyWindow != nil { 
     return self.topViewControllerWithRootViewController(UIApplication.sharedApplication().keyWindow!.rootViewController!) 
    } 
    return nil 
} 

func topViewControllerWithRootViewController(rootViewController: UIViewController?) -> UIViewController? { 
    if rootViewController == nil { 
     return nil 
    } 
    if rootViewController!.isKindOfClass(UITabBarController) { 
     let tabBarController: UITabBarController = (rootViewController as? UITabBarController)! 
     return self.topViewControllerWithRootViewController(tabBarController.selectedViewController) 
    } 
    else { 
     if rootViewController!.isKindOfClass(UINavigationController) { 
      let navigationController: UINavigationController = (rootViewController as? UINavigationController)! 
      return self.topViewControllerWithRootViewController(navigationController.visibleViewController) 
     } 
     else { 
      if (rootViewController!.presentedViewController != nil) { 
       let presentedViewController: UIViewController = rootViewController!.presentedViewController! 
       return self.topViewControllerWithRootViewController(presentedViewController) 
      } 
      else { 
       return rootViewController 
      } 
     } 
    } 
} 

目前這似乎工作。

0

你試過這個..嗎?

let root = UIApplication.sharedApplication().delegate! as! AppDelegate 
let storyboard = UIStoryboard(name: "Main", bundle: nil) 
let pvc = storyboard.instantiateViewControllerWithIdentifier("UserProfile") as! UserProfile 
pvc.modalPresentationStyle = UIModalPresentationStyle.FullScreen 
root.window?.rootViewController?.presentViewController(pvc, animated: true , completion: nil) 
+0

Thx Vatsal - 不幸的是,這也似乎不工作 - 我想知道如果視圖被放置在其他視圖下,但無法找到一種方法將其帶到前面 – RobertyBob

+0

完成處理程序顯示它並沒有放置在其他地方,但絕對沒有被呈現出來 – RobertyBob

相關問題