![](/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.