簡體   English   中英

如何預加載或緩存目標viewcontroller而不用segue實例化

[英]how to preload or cache destination viewcontroller without instantiate by segue

當觸發segue時,它將啟動目標視圖控制器。 有沒有辦法我可以預先加載或緩存視圖控制器並在觸發segue時將其加載到代碼中? 提前致謝。

使用segue的一個好處是視圖控制器是為您設置和實例化的。 您可以簡單地不使用segue,並自己將VC啟動/推送到導航堆棧。 這將允許您隨時初始化它,將VC存儲為ivar,然后在適當時將其推送。

是。 例如,為了提高響應速度,我需要預先加載旋轉設備時顯示的照片。 因此,在故事板中,我選擇了橫向VC並在Identity Inspector中設置了Storyboard ID。 然后當縱向VC加載時我通過調用創建了風景VC:

self.landscapeViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"landscapePhotosVC"];
// this next line is critical so photos can be added to the UIScrollView. 
// Else the UIScrollView on landscape VC will be nil
[self.landscapeViewController view];
self.landscapeViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
self.landscapeViewController.modalPresentationStyle = UIModalPresentationFullScreen;

然后當viewDidAppear觸發縱向VC時,我將照片添加到橫向VC上的UIScrollView。

當設備旋轉到橫向時,我會顯示風景VC:

[self.navigationController presentViewController:self.landscapeViewController 
                                        animated:YES 
                                      completion:nil];

您可以在沒有segue的情況下實例化視圖控制器,首先在storyboard編輯器中為UIViewController設置storyboardID,然后使用

MyViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"MyVC"];

暫無
暫無

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

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