NavigationController 中的 UIPageViewController [英] UIPageViewController within NavigationController

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

问题描述

我已经阅读了我找到的所有关于 UIPageViewController 的教程,但它们只展示了基础知识,我想创建类似于新的 twitter 应用程序的东西:

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 嵌入到 Navigation 控制器中,导航栏的标题基于当前页面,并且页面点也在那里,用户可以点击当前页面上的项目(来自表格视图/集合视图的项目)以查看详细信息.

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

最后一个链接专门用于根据您正在查看的内容设置导航栏的内容.

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

关键是在您的 UIPageViewController 内容视图控制器的 .h 文件中创建一个 UINavigationItem 属性,这意味着显示您正在显示的任何内容的那些/一个.

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.hThirdViewController.h

@property (strong, nonatomic) UINavigationItem *navItem;  

在上面的第二个和第三个链接中,您将看到主从应用程序(使用导航控制器)的故事板布局.UIPageViewControllerDataSourceDetailViewController.与 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.将其传入可确保您可以更改导航栏项目.

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 很重要,因为每次屏幕出现时都不会调用 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.

如果您像教程一样为所有页面使用单个视图控制器,您将不得不使用 index 属性来知道将标题设置为什么.

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天全站免登陆