iOS - 预加载页面视图控制器及其子视图控制器

时间:2015-05-25 18:02:18

标签: ios objective-c

当我的应用程序的标签视图加载时,我正在尝试加载页面视图控制器并将其保存在应用程序的生命周期内。页面View是我的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
                                     }];

2 个答案:

答案 0 :(得分:0)

实现这一目标的一种方法是访问viewcontroller的视图框架。

CGRect dontCareRect = myNewViewController.view.frame

这会强制加载vc的视图。

答案 1 :(得分:0)

如果你真的需要预加载它,请在ViewController上调用.view,这会强制加载。

预加载视图不一定是必要的。您可以设置子视图的加载动画,以增强用户体验并保持性能。