2015-03-03 49 views
0

我的主控制器有一个UIPageViewController。我想滑过不同结构的视图,所以我不能使用相同的控制器。另外,我不想污染主要的故事板。我想用自定义控制器来使用不同的XIB文件。到现在为止还挺好。IOS - 如何用不同的XIB文件创建具有多个ViewController的UIPageViewController

我想能够提供索引到UIPageViewController。

This答案显示了如何使用多个视图控制器完成它,但它们都在故事板内。

我试过同样的方法。我用相应的ViewController创建了我的XIB。为了向PageViewController提供索引,我尝试将RestorationId设置为该视图以稍后在代码中访问。 在PageViewController,我建立控制器是这样的:

func viewControllerAtIndex(index: Int) -> UIViewController { 
    let vc = UIViewController(nibName: controllersNames[index], bundle: nil) 
    ... 
} 

但如果我这样做

vc.restorationIdentifier 

我得到零..

所以,我似乎无法找到一种方法,将控制器绑定到我需要提供UIPageViewController的索引。

请帮忙。

回答

2

您需要像创建主视图控制器一样创建,并在此视图控制器中初始化所有您需要的VC。

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     // Custom initialization 
     // initialize controllers 
     self.controllers = [[NSMutableArray alloc] 
          initWithObjects: 
          [[Document1ViewController alloc] initWithNibName:@"Document1ViewController" bundle:nil], 
          [[Document2ViewController alloc] initWithNibName:@"Document2ViewController" bundle:nil], 
          [[Document3ViewController alloc] initWithNibName:@"Document3ViewController" bundle:nil], 
          nil]; 
    } 
    return self; 
} 

然后,你需要创建一个与你想要的VC数量分页的滚动视图。

- (void)viewWillAppear:(BOOL)animated { 
    [super viewWillAppear:animated]; 

    for (NSUInteger i =0; i < [self.controllers count]; i++) { 
     [self loadScrollViewWithPage:i]; 
    } 

    self.pageControl.currentPage = 0; 
    _page = 0; 
    [self.pageControl setNumberOfPages:[self.controllers count]]; 

    UIViewController *viewController = [self.controllers objectAtIndex:self.pageControl.currentPage]; 
    if (viewController.view.superview != nil) { 
     [viewController viewWillAppear:animated]; 
    } 

    self.scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * [self.controllers count], scrollView.frame.size.height); 
} 

滚动时,你需要更改滚动视图的VC订单,可能是因为你有所有VC的引用一个数组。

你可以看到一个例子here

相关问题