简体   繁体   English

如何在UIPageViewController中的UIViewControllers之间传递数据并保存变量

[英]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. 我在弄清楚如何在UIPageViewController中的UIViewController之间保存/传递数据时遇到了麻烦。 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). 这在我的RootViewController.m(包含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. 如果您只需要来回传递少量信息并且需要同时将其传递到多个目的地,则NSNotificationCenter可能是最快的实现。 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 . 快速的Google搜索将本演练作为第一个结果 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. 要在NSNotificationCenter运行可能需要花费额外的几分钟时间,但是它更具扩展性,可读性和可维护性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在UIViewControllers之间传递数据 - pass data between UIViewControllers 如何在UIViewControllers与协议/委托之间传递数据 - How to pass data between UIViewControllers with protocols/delegates 两个UIViewController之间的UIPageViewController - UIPageViewController between two UIViewControllers 如何在不同的UIViewControllers之间传递数据? - How to pass data between different UIViewControllers? 如何在UIViewControllers之间传递数据而不显示它 - How to pass data between UIViewControllers without presenting it 如何在UIPageViewController中的两个ViewController之间传递数据 - How to pass data between two ViewController in UIPageViewController 如何通过@objc函数在UIViewControllers之间传递数据数组 - How to pass array of data between UIViewControllers from @objc function 如何在布局类似于叉子的UIViewControllers之间导航和传递数据? - How to navigate and pass data between UIViewControllers whose layout resembles a fork? 如何从属于 UIPageViewController 的 UIViewControllers 之一中更改 UIPageViewController? - How do I change the UIPageViewController from WITHIN one of the UIViewControllers that is part of the UIPageViewController? 如何在UITabBarController中的两个UIViewControllers之间传递信息 - How to pass information between two UIViewControllers in a in a UITabBarController
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM