![](/img/trans.png)
[英]Xamarin iOS how to make different xib for iPad and iPhone with same ViewController
[英]IOS - How to create a UIPageViewController with multiple ViewController from different XIB files
我的主控制器有一个UIPageViewController。 我想在结构不同的视图上滑动,因此不能使用相同的控制器。 另外,我不想污染主故事板。 我想要具有自定义控制器的不同XIB文件。 到现在为止还挺好。
我希望能够提供UIPageViewController的索引。
这个答案显示了如何使用多个视图控制器来实现,但是它们都在情节提要中。
我尝试过相同的方法。 我用相应的ViewControllers创建了XIB。 为了提供PageViewController的索引,我尝试将RestorationId设置为视图以在代码中稍后访问。 在PageViewController中,我像这样构建控制器:
func viewControllerAtIndex(index: Int) -> UIViewController {
let vc = UIViewController(nibName: controllersNames[index], bundle: nil)
...
}
但是如果我这样做
vc.restorationIdentifier
我得到零
所以我似乎找不到找到将控制器绑定到需要提供UIPageViewController的索引的方法。
请帮忙。
您需要像主视图控制器一样进行创建,并在此视图控制器中初始化所需的所有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);
}
滚动时,您需要在scrollView中更改VC顺序,这是可能的,因为您有一个包含所有VC引用的数组。
你可以在这里看到一个例子
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.