簡體   English   中英

如何等待視圖進入視圖層次結構?

[英]How do I wait for a view to come into the view hierarchy?

在刪除一些發出網絡請求的代碼之前,我的應用程序一直在工作(因此這導致了幾秒鍾的等待)。 我想這是將視圖加載到窗口層次結構上的足夠時間...但是現在,我刪除了這個網絡請求,並且可以直接嘗試打開該視圖,我收到以下消息:

警告:嘗試顯示不在窗口層次結構中的視圖!

我如何才能等到該視圖出現后再嘗試打開它?

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
StoreFlyersViewController * vc = [storyboard instantiateViewControllerWithIdentifier:@"TabBar"];
[self presentViewController:vc animated:NO completion:nil];

糾正我,如果我錯了,但似乎你正在推動/呈現視圖控制器(稱為A ),另一種(在StoreFlyersViewController )立即,例如,從viewDidLoad的方法A -時的觀點A不在窗口層次結構呢。

為了解決這個問題,您可以將代碼移至viewDidAppear: 或者,如果這沒有產生您想要的結果,則可以嘗試將代碼移至單獨的方法,然后通過performSelector:withObject:afterDelay:短暫延遲(例如0.1秒)來調用該方法。

嘗試這個;

dispatch_async(dispatch_get_main_queue(), ^{
       UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
StoreFlyersViewController * vc = [storyboard instantiateViewControllerWithIdentifier:@"TabBar"];
[self presentViewController:vc animated:NO completion:nil];
    });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM