xcode 在故事板中使用 UIPageViewController
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10115257/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
using the UIPageViewController in a storyboard
提问by Reza Shirazian
I'm looking to use the UIPageViewController
with Xcode's storyboard. The only example I could find was this tutorialon how to implement UIPageViewcontroller
using separate xib files.
我正在寻找UIPageViewController
与 Xcode 的故事板一起使用。我能找到的唯一示例是有关如何使用单独的 xib 文件实现的教程UIPageViewcontroller
。
However in this tutorial the UIPageViewController
is instantiated programmatically within a class that extends UIViewController
. This makes it complicated to translate in to how the storyboard interprets the UIPageViewcontroller
(which is as an instance on its own).
但是,在本教程中,UIPageViewController
以编程方式在扩展UIViewController
. 这使得转换为故事板如何解释UIPageViewcontroller
(作为一个实例本身)变得很复杂。
Any help on how to create a functioning UIPageViewController
with Xcode's storyboard will be much appreciated.
任何有关如何UIPageViewController
使用 Xcode 的故事板创建功能的帮助将不胜感激。
UPDATE:I managed to resolve this issue by making a new project in Xcode using the default pagecontroller template. It used the storyboard and was easy to follow.
更新:我设法通过使用默认的页面控制器模板在 Xcode 中创建一个新项目来解决这个问题。它使用情节提要并且易于遵循。
回答by Reza Shirazian
UPDATE: I managed to resolve this issue by making a new project in xcode using the default pagecontroller template. It used the storyboard and was easy to follow.
更新:我设法通过使用默认的 pagecontroller 模板在 xcode 中创建一个新项目来解决这个问题。它使用情节提要并且易于遵循。
回答by NSNewYorkMets
Setting up the Xcode Project to be a PageViewController is one way of accomplishing this, however if you'd like to include a PageViewController within an already existing storyboard you can do this as well.
将 Xcode 项目设置为 PageViewController 是实现此目的的一种方法,但是如果您想在现有故事板中包含一个 PageViewController,您也可以这样做。
If you drag and drop a PageViewController Scene onto your storyboard and wire up a segue, you can push to that PageViewController. However there appears to be some bugs in the storyboard for setting up the PageViewController properly, for instance you cannot wire up the delegate and datasource.
如果您将 PageViewController 场景拖放到故事板上并连接 segue,则可以推送到该 PageViewController。然而,故事板中似乎存在一些错误,用于正确设置 PageViewController,例如您无法连接委托和数据源。
An easy way around this is to simply wire the delegate/datasource up in your init method:
解决此问题的一种简单方法是在您的 init 方法中简单地连接委托/数据源:
- (instancetype) initWithCoder:(NSCoder *)aDecoder
{
if(self = [super initWithCoder:aDecoder])
{
self.delegate = self;
self.dataSource = self;
}
return self;
}
This will cause your delegate and datasource methods to be called properly, assuming of course you want the datasource and delegates to be the PageViewController. Once this is set up you will need to ensure that there is a view controller when the view is loaded. You can do this with the setViewControllers method on PageViewController class in your viewDidLoad:
这将导致您的委托和数据源方法被正确调用,当然假设您希望数据源和委托是 PageViewController。设置完成后,您需要确保在加载视图时有一个视图控制器。您可以使用 viewDidLoad 中 PageViewController 类上的 setViewControllers 方法执行此操作:
- (void)viewDidLoad
{
[super viewDidLoad];
[self setViewControllers:@[sweetViewController] direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:^(BOOL finished) {
NSLog(@"Hooray I set my initial viewcontroller for my page view controller");
}];
}
When the PageViewController is created, it will start with your sweetViewController, and then begin calling your datasource and delegate methods as needed.
创建 PageViewController 后,它将以您的 sweetViewController 开始,然后根据需要开始调用您的数据源和委托方法。
回答by NoLongerContributingToSE
There's an easy way to do this with Xcode 7. Have the ViewController that will be your datasource and delegate in one Storyboard Scene, with your UIPageViewController embedded into a ContainerView. Then capture the UIPageViewController in your ViewController's prepareForSegue Method.
使用 Xcode 7 有一种简单的方法可以做到这一点。让 ViewController 成为您的数据源并在一个 Storyboard 场景中进行委托,并将您的 UIPageViewController 嵌入到 ContainerView 中。然后在您的 ViewController 的 prepareForSegue 方法中捕获 UIPageViewController。
e.g. in Swift:
例如在斯威夫特:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if let identifier = segue.identifier where identifier == "yourPageVCIdentifier"{
if let pageController = segue.destinationViewController as? UIPageViewController {
pageController.dataSource = self
pageController.delegate = self
self.pageVC = pageController
}
}
}