我无法弄清楚如何在UIPageViewController中的UIViewControllers之间保存/传递数据。我的设置是这样的:
#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;
}
这是在我的RootViewController.m(包含UIPageViewController的控制器)中。我需要做的是在刷新新控制器时,在当前显示的控制器中保存变量或数据。我是否需要使用单身或其他东西?
答案 0 :(得分:0)
尝试在* ViewController中使用NSNotificationCenter类
答案 1 :(得分:0)
NSNotificationCenter
可能是您最快的实现,如果您只需要来回传递小块信息,并且需要同时向多个目的地传递信息。如果在更可能的情况下,您需要经常更新信息并仅在必要时引用它,那么单例将是一个更实用的解决方案。
单身人士非常容易使用和实施。 A quick google search had this walkthrough as the first result。这将向您展示如何设置它,并且使用它非常类似于在视图控制器中使用任何其他属性。
可能需要花费几分钟的时间来运行NSNotificationCenter
,但它更具可扩展性,可读性和可维护性。