Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 从包含在UIPageViewController中的视图设置UINavigationBar标题_Ios_Objective C_Uinavigationbar_Uipageviewcontroller - Fatal编程技术网

Ios 从包含在UIPageViewController中的视图设置UINavigationBar标题

Ios 从包含在UIPageViewController中的视图设置UINavigationBar标题,ios,objective-c,uinavigationbar,uipageviewcontroller,Ios,Objective C,Uinavigationbar,Uipageviewcontroller,这就是我遇到的问题。我无法为包含在UIPageViewController中的视图设置UINavigationBar标题 应用程序的基本架构如下所示 应用程序的根视图控制器是一个UITabBarController,其中包含5个导航控制器 我遇到问题的第一个导航控制器包含一个页面视图控制器,而此页面视图控制器包含多个UIViewController 我希望,当我在每个视图控制器中滑动时,我可以在UINavigationBar中设置标题 我尝试了以下方法: 在页面视图控制器中包含的UIViewCo

这就是我遇到的问题。我无法为包含在UIPageViewController中的视图设置UINavigationBar标题

应用程序的基本架构如下所示

  • 应用程序的根视图控制器是一个UITabBarController,其中包含5个导航控制器

  • 我遇到问题的第一个导航控制器包含一个页面视图控制器,而此页面视图控制器包含多个UIViewController

  • 我希望,当我在每个视图控制器中滑动时,我可以在UINavigationBar中设置标题

  • 我尝试了以下方法:

  • 在页面视图控制器中包含的UIViewController中,我尝试了
    [self-setTitle:@“Title I-want”]
    -它不起作用

  • 在同一个UIViewController中,我还尝试了
    [self.navigationBar.navigationItem setTitle:@“Title I want”]
    -这也不起作用

  • 我还尝试设置视图控制器的标题,并尝试在PageViewControllers委托方法transitionCompleted中提取该标题,但这也不起作用

    我想知道我是否应该回到绘图板上,以及我是否会因为这个视图布局架构而陷入困境。是否有其他人遇到过此问题?如果是,您是如何解决的

    编辑:我还必须补充一点,我是按程序来做这件事的


    谢谢您的帮助。

    我认为您不应该在导航栏上设置标题,您是否尝试过
    self.navigationController.title=@“title”

    所以,最后我想出了一个方法来让它工作,尽管这不是我想要的最干净的解决方案,但仍然适合这个目的

  • 我创建了一个名为PageLeafViewController的新类,并设置了它的init方法,如下所示。页视图控制器的子视图控制器从此继承。这是代码
  • 代码示例

    - (id)initWithIndex:(NSUInteger)index andTitle:(NSString *)navBarTitle; {
        if(self = [super init]) {
            self.index = index;
            self.navBarTitle = navBarTitle;
        }
        return  self;
    }
    
    ChildViewController *aChildViewController = [[ChildViewController alloc] initWithIndex:1 andTitle:@"A Title"];
    
    PageLeafViewController *initialViewController = (PageLeafViewController *)[self viewControllerAtIndex:0];
    [self.navigationItem setTitle:initialViewController.navBarTitle];
    
    - (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed {
    
        PageLeafViewController *currentLeaf = (PageLeafViewController *)[self.pageViewController.viewControllers lastObject];
        [self.navigationItem setTitle:currentLeaf.navBarTitle];
    }
    
  • 在添加到UIPageViewController之前,可以对其进行初始化
  • 代码示例

    - (id)initWithIndex:(NSUInteger)index andTitle:(NSString *)navBarTitle; {
        if(self = [super init]) {
            self.index = index;
            self.navBarTitle = navBarTitle;
        }
        return  self;
    }
    
    ChildViewController *aChildViewController = [[ChildViewController alloc] initWithIndex:1 andTitle:@"A Title"];
    
    PageLeafViewController *initialViewController = (PageLeafViewController *)[self viewControllerAtIndex:0];
    [self.navigationItem setTitle:initialViewController.navBarTitle];
    
    - (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed {
    
        PageLeafViewController *currentLeaf = (PageLeafViewController *)[self.pageViewController.viewControllers lastObject];
        [self.navigationItem setTitle:currentLeaf.navBarTitle];
    }
    
  • 您需要将UIPageViewControllerDelegate添加到页面视图控制器的界面中。这样,您就可以在视图转换完成时实现委托方法的代码,并且需要设置标题

  • 加载UIPageViewController时,我抓取第一个视图控制器并获取其标题,将其设置为UINavigationController导航栏

  • 代码示例

    - (id)initWithIndex:(NSUInteger)index andTitle:(NSString *)navBarTitle; {
        if(self = [super init]) {
            self.index = index;
            self.navBarTitle = navBarTitle;
        }
        return  self;
    }
    
    ChildViewController *aChildViewController = [[ChildViewController alloc] initWithIndex:1 andTitle:@"A Title"];
    
    PageLeafViewController *initialViewController = (PageLeafViewController *)[self viewControllerAtIndex:0];
    [self.navigationItem setTitle:initialViewController.navBarTitle];
    
    - (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed {
    
        PageLeafViewController *currentLeaf = (PageLeafViewController *)[self.pageViewController.viewControllers lastObject];
        [self.navigationItem setTitle:currentLeaf.navBarTitle];
    }
    
  • 当转换发生时,当转换到视图完成时,我们再次将标题设置为新的子视图控制器的标题
  • 代码示例

    - (id)initWithIndex:(NSUInteger)index andTitle:(NSString *)navBarTitle; {
        if(self = [super init]) {
            self.index = index;
            self.navBarTitle = navBarTitle;
        }
        return  self;
    }
    
    ChildViewController *aChildViewController = [[ChildViewController alloc] initWithIndex:1 andTitle:@"A Title"];
    
    PageLeafViewController *initialViewController = (PageLeafViewController *)[self viewControllerAtIndex:0];
    [self.navigationItem setTitle:initialViewController.navBarTitle];
    
    - (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed {
    
        PageLeafViewController *currentLeaf = (PageLeafViewController *)[self.pageViewController.viewControllers lastObject];
        [self.navigationItem setTitle:currentLeaf.navBarTitle];
    }
    
    注意:当显示新的子视图控制器时,会自动调用上述命令

    虽然这不是目前最优雅的解决方案,但我认为不可能从子视图中调用函数来更新导航栏标题,除非有人想纠正我


    希望这能有所帮助。

    如果您以编程方式使用选项卡栏,则可以在将控制器添加到选项卡栏时设置标题,我认为您只需执行
    ViewController.title=@“title”
    VC.view.title
    然后将其添加到控制器阵列底部的选项卡栏很好,我正在设置它们的标题。诀窍是找出如何让ViewController告诉navigationbar它需要什么标题,这样当用户滑动时,它可以很好地更改。