简体   繁体   中英

How should I pass data between UIViewControllers in UIPageViewController and save variables

I am having trouble figuring out how to save/pass data between UIViewControllers in UIPageViewController. My setup is like so:

#pragma mark - UIPageViewControllerDataSource

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
{

    //This is nice and avoids having to use a counter
    NSString *vcRestorationID = viewController.restorationIdentifier;
    NSUInteger index = [self.controllerRestorationIDs indexOfObject:vcRestorationID];

    if (index == 0) {
        return nil;
    }

    return [self viewControllerAtIndex:index - 1];
}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
{
    NSString *vcRestorationID = viewController.restorationIdentifier;
    NSUInteger index = [self.controllerRestorationIDs indexOfObject:vcRestorationID];

    //Don't allow it to go forward if there is one at the end
    if (index == self.controllerRestorationIDs.count - 1) {
        return nil;
    }

    return [self viewControllerAtIndex:index + 1];
}


#pragma mark - Private Methods

- (UIViewController *)viewControllerAtIndex:(NSUInteger)index
{
    // Only process a valid index request.
    if (index >= self.controllerRestorationIDs.count) {
        return nil;
    }

    // Create a new view controller.
    //Note this is just an extension of UIViewController with a variable inside. All my view controllers in this must be subclassed off BaseContentViewController
    BaseContentViewController *contentViewController = (BaseContentViewController *)[self.storyboard instantiateViewControllerWithIdentifier:self.controllerRestorationIDs[index]];

    // Set any data needed by the VC here
    contentViewController.rootViewController = self;

    return contentViewController;

}

This is in my RootViewController.m (the controller that contains UIPageViewController). What I need to be able to do is so save a variable or data in the current displayed controller when a new one is swiped to. Do I need to use a singleton or something for this?

尝试在* ViewController中使用NSNotificationCenter类

NSNotificationCenter will probably be your fastest implementation if you only ever have to pass small pieces of information back and forth and need to do so to multiple destinations simultaneously. If, in the far more likely instance, you need to update information frequently and reference it only when necessary then a singleton would be a much more practical solution.

Singletons are surprisingly easy to use and implement. A quick google search had this walkthrough as the first result . That will show you how to set it up, and using it is very similar to using any other property in a view controller.

It may take a couple extra minutes of work to get running over NSNotificationCenter but it's more extensible, readable, and maintainable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM