NavigationController中的UIPageViewController [英] UIPageViewController within NavigationController

查看:124
本文介绍了NavigationController中的UIPageViewController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了我发现的关于UIPageViewController的每个教程,但它们只显示基础知识,我想创建类似新推特应用程序的东西:

I've read every tutorial I've found about UIPageViewController, but they show just basics, I'd like to create something like new twitter app has:

UIPageViewController嵌入到导航中控制器,导航栏的标题基于当前页面,那些页面点也在那里,用户可以点击当前页面上的项目(项目视图/集合视图中的项目)来查看详细信息。

UIPageViewController is embedded into Navigation controller, title of navigation bar is based on current page and those page dots are there as well, user can tap on item on current page(item from table view/collection view) to see detail.

我能够得到类似的东西,每个页面都有收集视图,并显示某些项目的细节反映在导航栏中,有正确的标题和<按钮,但我无法根据当前显示的页面更改标题

I was able to come up with something similar, each page had collection view, and showing detail of some item was reflected in navigation bar, there was correct title and "<" button, but I wasn't able to change title based on currently shown page

请您,您能用几步/控制器的基本结构来描述如何执行此操作吗?

Please, could you describe me how to do this in few steps/basic structure of controllers?

推荐答案

不知道你是否还在为此工作,但无论如何都要去。要设置UIPageViewController,您可以使用本教程和下面两个问题。

Don't know if you are still working on this but here goes anyway. To set up a UIPageViewController you might use the tutorial and two questions below.

http://www.appcoda.com/uipageviewcontroller-storyboard-tutorial/

如何实现利用多个ViewControllers的UIPageViewController

如何在使用UIPageViewController时将UIBarButtonItem添加到NavigationBar

最后一个链接专门用于根据您正在查看的内容设置navigationBar的内容。

The last link pertains specifically to setting the contents of the navigationBar depending on what you are viewing.

关键是在UIPageViewController内容视图控件的.h文件中创建UINavigationItem属性lers,意思是那些显示你正在显示的内容的那些。

The key is to create a UINavigationItem Property in the .h file of your UIPageViewController content view controllers, meaning the ones/one that are displaying whatever it is you are displaying.

来自我的代码 FirstViewController.h SecondViewController.h ThirdViewController.h

@property (strong, nonatomic) UINavigationItem *navItem;  

在上面的第二个和第三个链接中,您将看到Master-Detail应用程序的故事板布局(它使用导航控制器)。 UIPageViewControllerDataSource DetailViewController 。与 pageViewController 相关联的三个页面是我的内容视图控制器。

In the second and third links above you'll see a storyboard layout of a Master-Detail application (which uses a navigation controller). The UIPageViewControllerDataSource is the DetailViewController. The three pages associated with the pageViewController are my content view controllers.

在DetailViewController.m中,您必须实例化contentViewControllers在某个地方。此时,您将DetailViewControllers navigationItem id传递给内容视图控制器。以下是使用 UIPageViewController 的委托方法实例化我的内容视图控制器的方法。

In DetailViewController.m you have to instantiate the contentViewControllers somewhere. At that point you pass the DetailViewControllers navigationItem id to the content view controllers. Here is how I instantiate my content view controllers using the delegate methods of the UIPageViewController.

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
{

    NSString * ident = viewController.restorationIdentifier;
    NSUInteger index = [_vc indexOfObject:ident];

    if ((index == 0) || (index == NSNotFound)) {
        return nil;
    }

    index--;

    if (index == 0) {
        return [self controllerAtIndex:index];
    }else if (index == 1){
        return [self secondControllerAtIndex:index];
    }else if (index == 2){
        return [self thirdControllerAtIndex:index];
    }else{
        return nil;
    }
}

委托方法调用以下方法。它几乎直接来自教程链接,只做了一些修改。

The delegate method calls the method below. It is almost directly from the tutorial link with just a few modifications.

-(FirstController *)controllerAtIndex:(NSUInteger)index
{
    FirstController *fvc = [self.storyboard instantiateViewControllerWithIdentifier:@"FirstPageController"];
    fvc.imageFile = self.pageImages[index];
    fvc.titleText = self.pageTitles[index];
    fvc.pageIndex = index;
    fvc.navItem = self.navigationItem;
    return fvc;
}

请注意,属性传递到视图控制器,包括 self.navigationItem 。传入它确保您可以更改navigationBar项目。

Notice that properties are passed into the view controller including self.navigationItem. Passing it in ensures you can make changes to the navigationBar items.

然后在内容视图的 viewDidAppear 方法中控制器你可以像这样在导航栏上设置标题。

Then in the viewDidAppear method of your content view controller you can simply set the title on the navigation bar like this.

navItem.navigationItem.title = @"Whatever you want the title to be";

使用 viewDidAppear 非常重要因为<每次出现屏幕时都不会调用code> viewDidLoad 。我相信UIPageViewController会在您查看之前缓存页面之前和页面,从而使系统不必在每次导航时都加载页面。

It is important to use viewDidAppear because viewDidLoad is not called every time the screen appears. I believe the UIPageViewController caches the page before and the page after whatever you are viewing which saves the system from having to load the page every time you navigate to it.

如果你正在为所有页面使用单个视图控制器,就像教程一样,您必须使用索引属性来知道要将标题设置为什么。

If you are using a single view controller for all you pages like the tutorial does you will have to use the index property to know what to set the title to.

希望这会有所帮助!

这篇关于NavigationController中的UIPageViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆