[英]How to add the same child View controllers to a parent view controller in iOS?
[英]iOS - Preloading a Page View Controller and its child view controllers
加載應用程序的選項卡視圖時,我還將嘗試加載頁面視圖控制器並將其保存在內存中,以保持應用程序的生命周期。 頁面視圖是我的self.playerQueueViewController。
-(void)viewDidLoad{
[super viewDidLoad];
self.playerQueueViewController = [[UIStoryboard storyboardWithName:@"MusicPlayerQueue" bundle:nil] instantiateInitialViewController];
self.playerQueueViewController.transitioningDelegate = self;
self.playerQueueViewController.modalPresentationStyle = UIModalPresentationCustom;
}
現在,只有在我致電時,視圖才會加載,
-(void)centerButtonPressed:(id)sender
{
[self presentViewController:self.playerQueueViewController animated:YES completion:nil];
}
如何在我的tabViewController加載后立即加載和加載頁面視圖控制器的子視圖控制器的視圖? 我試圖將所有這些視圖控制器保存在內存中。
這是我設置pageViewController的方法。
當前正在發生的事情是,僅當我使用-(void)centerButtonPressed模態顯示pageViewController時,才會加載它。 當我調用-(void)goToPrevious,-(void)goToNext時,子視圖會被加載
@property (strong, nonatomic) SAMusicPlayerViewController *playerViewController;
@property (strong, nonatomic) SAQueueViewController *queueViewController;
@end
@implementation SAPageRootViewController
- (IBAction)dissmissArrowPressed:(UIButton *)sender
{
[self dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark - Setters and Getters
- (NSArray *)contentPageRestorationIDs
{
if (!_contentPageRestorationIDs) {
_contentPageRestorationIDs = @[@"Player", @"Queue",];
}
return _contentPageRestorationIDs;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Create the page view controller.
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MusicPlayerQueue" bundle:nil];
self.pageViewController = [storyboard instantiateViewControllerWithIdentifier:@"PageViewController"];
self.queueViewController = [storyboard instantiateViewControllerWithIdentifier:@"Queue"];
self.queueViewController.rootViewController = self;
self.playerViewController = [storyboard instantiateViewControllerWithIdentifier:@"Player"];
self.playerViewController.rootViewController = self;
NSLog(@"Page Root Load");
// Instantiate the first view controller.
[self.pageViewController setViewControllers:@[self.playerViewController]
direction:UIPageViewControllerNavigationDirectionForward
animated:NO
completion:^(BOOL finished) {
// Completion code
}];
// Add the page view controller to this root view controller.
[self addChildViewController:self.pageViewController];
[self.view addSubview:self.pageViewController.view];
[self.pageViewController didMoveToParentViewController:self];
[self.view bringSubviewToFront:self.dismissArrowButton];
}
#pragma mark - Public Methods
- (void)goToPreviousContentViewController
{
// Get index of current view controller
SAQueueViewController *currentViewController = self.queueViewController;
currentViewController.currentButton.enabled = NO;
UIViewController *previousViewController = self.playerViewController;
[self.pageViewController setViewControllers:@[previousViewController]
direction:UIPageViewControllerNavigationDirectionReverse
animated:YES
completion:^(BOOL finished) {
currentViewController.currentButton.enabled = YES;
}];
}
- (void)goToNextContentViewController
{
// Get index of current view controller
SAMusicPlayerViewController *currentViewController = self.playerViewController;
currentViewController.queueButton.enabled = NO;
UIViewController *nextViewController = self.queueViewController;
[self.pageViewController setViewControllers:@[nextViewController]
direction:UIPageViewControllerNavigationDirectionForward
animated:YES
completion:^(BOOL finished) {
currentViewController.queueButton.enabled = YES;
}];
}
更新:
現在,我可以像這樣加載pageViewController的頁面視圖和初始子視圖。 問題是我無法在初始加載時加載第二個子視圖控制器。
所有這些代碼都在我的tabViewController的viewDidLoad中
self.pageRootViewController = [[UIStoryboard storyboardWithName:@"MusicPlayerQueue" bundle:nil] instantiateInitialViewController];
self.pageRootViewController.transitioningDelegate = self;
self.pageRootViewController.modalPresentationStyle = UIModalPresentationCustom;
UIPageViewController *pageViewController = self.pageRootViewController.pageViewController;
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MusicPlayerQueue" bundle:nil];
pageViewController = [storyboard instantiateViewControllerWithIdentifier:@"PageViewController"];
self.queueViewController = [storyboard instantiateViewControllerWithIdentifier:@"Queue"];
self.queueViewController.rootViewController = self.pageRootViewController;
self.playerViewController = [storyboard instantiateViewControllerWithIdentifier:@"Player"];
self.playerViewController.rootViewController = self.pageRootViewController;
// Add the page view controller to this root view controller.
[self.pageRootViewController addChildViewController:pageViewController];
[self.pageRootViewController.view addSubview:pageViewController.view];
[pageViewController didMoveToParentViewController:self.pageRootViewController];
[self.pageRootViewController.view bringSubviewToFront:self.pageRootViewController.dismissArrowButton];
// Instantiate the first view controller.
[pageViewController setViewControllers:@[self.playerViewController, self.queueViewController]
direction:UIPageViewControllerNavigationDirectionReverse
animated:NO
completion:^(BOOL finished) {
// Completion code
}];
更新2:
好的,我的代碼完全按照我的要求工作。 但是,我不確定這是否是解決此問題的最佳方法。 好像還好 我唯一不喜歡的是調用[self.queueViewController view]
在tabBarController的viewDidLoad中
self.pageRootViewController = [[UIStoryboard storyboardWithName:@"MusicPlayerQueue" bundle:nil] instantiateInitialViewController];
self.pageRootViewController.transitioningDelegate = self;
self.pageRootViewController.modalPresentationStyle = UIModalPresentationCustom;
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MusicPlayerQueue" bundle:nil];
UIPageViewController *pageViewController = [storyboard instantiateViewControllerWithIdentifier:@"PageViewController"];
self.pageRootViewController.pageViewController = pageViewController;
self.queueViewController = [storyboard instantiateViewControllerWithIdentifier:@"Queue"];
self.queueViewController.rootViewController = self.pageRootViewController;
[self.queueViewController view];
self.playerViewController = [storyboard instantiateViewControllerWithIdentifier:@"Player"];
self.playerViewController.rootViewController = self.pageRootViewController;
self.pageRootViewController.queueViewController = self.queueViewController;
self.pageRootViewController.playerViewController = self.playerViewController;
// Add the page view controller to this root view controller.
[self.pageRootViewController addChildViewController:pageViewController];
[self.pageRootViewController.view addSubview:pageViewController.view];
[pageViewController didMoveToParentViewController:self.pageRootViewController];
[self.pageRootViewController.view bringSubviewToFront:self.pageRootViewController.dismissArrowButton];
// Instantiate the first view controller.
[pageViewController setViewControllers:@[self.playerViewController]
direction:UIPageViewControllerNavigationDirectionReverse
animated:NO
completion:^(BOOL finished) {
// Completion code
}];
一種做到這一點的方法(看起來像是一種破解),但是可以工作,它是訪問視圖控制器視圖的框架。
CGRect dontCareRect = myNewViewController.view.frame
這將強制VC的視圖加載。
如果確實需要預加載,請在ViewController上調用.view,這將強制加載。
預加載視圖是不必要的。 您可以對子視圖的加載進行動畫處理,以增強UX並保持性能。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.