<bdo id='PrFvh'></bdo><ul id='PrFvh'></ul>

  1. <tfoot id='PrFvh'></tfoot>
    <i id='PrFvh'><tr id='PrFvh'><dt id='PrFvh'><q id='PrFvh'><span id='PrFvh'><b id='PrFvh'><form id='PrFvh'><ins id='PrFvh'></ins><ul id='PrFvh'></ul><sub id='PrFvh'></sub></form><legend id='PrFvh'></legend><bdo id='PrFvh'><pre id='PrFvh'><center id='PrFvh'></center></pre></bdo></b><th id='PrFvh'></th></span></q></dt></tr></i><div id='PrFvh'><tfoot id='PrFvh'></tfoot><dl id='PrFvh'><fieldset id='PrFvh'></fieldset></dl></div>
  2. <legend id='PrFvh'><style id='PrFvh'><dir id='PrFvh'><q id='PrFvh'></q></dir></style></legend>
    1. <small id='PrFvh'></small><noframes id='PrFvh'>

      使用 UIPageViewController 时如何将 UIBarButtonItem 添加到 NavigationB

      时间:2023-06-11

        <bdo id='JHP6C'></bdo><ul id='JHP6C'></ul>

        <small id='JHP6C'></small><noframes id='JHP6C'>

                  <tbody id='JHP6C'></tbody>
                <tfoot id='JHP6C'></tfoot>
                <i id='JHP6C'><tr id='JHP6C'><dt id='JHP6C'><q id='JHP6C'><span id='JHP6C'><b id='JHP6C'><form id='JHP6C'><ins id='JHP6C'></ins><ul id='JHP6C'></ul><sub id='JHP6C'></sub></form><legend id='JHP6C'></legend><bdo id='JHP6C'><pre id='JHP6C'><center id='JHP6C'></center></pre></bdo></b><th id='JHP6C'></th></span></q></dt></tr></i><div id='JHP6C'><tfoot id='JHP6C'></tfoot><dl id='JHP6C'><fieldset id='JHP6C'></fieldset></dl></div>
              • <legend id='JHP6C'><style id='JHP6C'><dir id='JHP6C'><q id='JHP6C'></q></dir></style></legend>
                本文介绍了使用 UIPageViewController 时如何将 UIBarButtonItem 添加到 NavigationBar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                请原谅我,关于如何以编程方式将 UIBarButtonItem 添加到 NavigationBar 已经有很多问题了,但我似乎无法找到任何适合我的情况的解决方案.

                Please forgive me, there are already a ton of questions on how to add a UIBarButtonItem to a NavigationBar programmatically but I just can't seem to get any of the solutions to work in my situation.

                这是一个简单的测试应用程序的布局,我一直在使用它来学习 UIPageViewController.

                Here is the layout of a simple test app I have been working with to learn the UIPageViewController.

                我的页面视图控制器与三个独特的视图控制器一起工作得很好.我现在想为每个视图控制器设置唯一的 rightBarButtonItems.只需这样做,我就可以轻松地在 DetailViewController 中设置一个通用的 barButtonItem.

                I have the page view controller working nicely with three unique viewControllers. I would now like to set unique rightBarButtonItems for each of the view controllers. I can easily set a common barButtonItem in the DetailViewController by simply doing this.

                UIBarButtonItem *newButton = [[UIBarButtonItem alloc] initWithTitle:@"Whatever" 
                                              style:UIBarButtonItemStylePlain 
                                              target:self 
                                              action:@selector(doSomething)];    
                
                self.navigationItem.rightBarButtonItem = newButton;
                

                鉴于我需要为 pageViewController 中的三个页面中的每一个页面设置一个唯一按钮,因此我无法在 detailViewController 中设置该按钮.我已经在三个控制器的 viewDidAppear 中尝试了上面的代码,但是按钮不会出现.我也尝试过以相同的方式创建按钮,然后像这样设置它,但没有运气.

                Given that I need a unique button for each of the three pages in the pageViewController I am not able to set the button in the detailViewController. I have tried the code above in viewDidAppear of the three controllers but the buttons will not appear. I have also tried creating the button in the same way and then setting it like this with no luck.

                DetailViewController *vc = [[DetailViewController alloc] init];
                vc.navigationItem.rightBarButtonItem = newButton;
                

                我知道我在这里很近,我只是不确定如何指定我需要向 NavigationController 的 NavigationBar 添加一个按钮,该按钮正在运行我的 contentViewControllers 嵌入的 detailViewController.任何帮助都会特别好片段.

                I know I'm close here, I'm just not sure how to specify I need to add a button to the NavigationBar of the NavigationController that is running the detailViewController that my contentViewControllers are embedded in. Any help would be great ESPECIALLY SNIPPETS.

                作为旁注,我尝试了一些其他方法,但这些方法都出现了问题.

                As a side note I have tried a few other methods that have come up problematic.

                • viewDidLoad 而不是 viewDidAppear 中设置按钮
                  • 这不起作用,因为每次从一个页面滑动到下一个页面时都不会调用 viewDidLoad 方法.然而, viewDidAppear 方法是,所以我试图在那里设置按钮.可能每次都调用 viewWillAppear 但我没有检查.
                  • Setting the button in viewDidLoad rather than viewDidAppear
                    • This will not work because the viewDidLoad method is not called everytime you swipe from one page to the next. The viewDidAppear method is however so I am trying to set the button there. It is possible viewWillAppear is called everytime but I haven't checked.
                    • 如果用户快速翻阅页面,按钮将无法更改或无法出现,这是有问题的.

                    解决方案

                    事实证明我已经接近了……我只需要在我的三个视图控制器中的每一个中创建一个 navigationItem 属性,而不是在 UIPageViewController 方法中创建按钮.当在委托方法中实例化控制器时,我只需将 navigationItem 属性设置为与 detailViewController 的属性相同.这样,在我的 viewDidApper 方法中,我可以创建按钮.然后在 viewWillDisappear 我将按钮设置为 nil.您也可以在 DetailViewControllerviewdidLoad 处创建按钮,然后更改各个 viewControllers 的 viewDidAppear 中的文本和操作.这是一个示例.

                    It turns out I was close… Rather than creating the button in the UIPageViewController methods I simply needed to create a navigationItem property in each of my three view controllers. When the controllers are instantiated in the delegate methods I simply set the navigationItem property equal to that of the detailViewController. That way in my viewDidApper methods I could create the button. Then at viewWillDisappear I set the button to nil. You can also just create the button at viewdidLoad of the DetailViewController and change the text and action in viewDidAppear of the individual viewControllers. Here is a sample.

                    在委托方法中

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

                    我根据 viewController 的索引调用了一个辅助方法.helper 方法在调用时实例化视图控制器.实例化后,我设置了一些属性,包括 navigationItem 属性.

                    I call a helper method based on the index of the viewController. The helper method instantiates the view controllers when it is called. Once instantiated I set some properties including the navigationItem property.

                    -(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;
                    
                    }
                    

                    然后在 viewController 的 viewDidAppear 中我只是这样做

                    Then in the viewDidAppear of the viewController I just do this

                    -(void)viewDidAppear:(BOOL)animated
                    {
                    
                        UIBarButtonItem *newButton = [[UIBarButtonItem alloc]
                                                   initWithTitle:@"Whatever" style:UIBarButtonItemStylePlain target:self action:@selector(doSomething)];
                        navItem.rightBarButtonItem = newButton;
                    
                    }
                    

                    推荐答案

                    如果每次更改查看的页面时都需要更改按钮,则必须在您尝试过的委托方法之一中完成.但是,我建议您不要创建新按钮并将其设置为导航项,而只需更改标题即可.

                    If you need the button to change each time you change the page you look at, it must be done in one of the delegate methods that you have tried. I suggest however, that instead of creating a new button and setting it to the navigation item, you simply change the title.

                    您可以在界面生成器中创建按钮并将其链接到 DetailViewController 中的属性,然后使用 UIControlStateNormal 调用 setTitle:forState每次进入新页面时按钮.

                    You can create the button in interface builder and link it to a property in your DetailViewController and then call setTitle:forState with UIControlStateNormal on the button every time you go to a new page.

                    如果您需要按钮为每个页面执行不同的操作,我建议您在按钮操作方法中检查当前页面,而不是每次都声明一个新页面.

                    If you need the button to do something different for each page, I would recommend checking the current page in the buttons action method rather than declaring a new one each time.

                    这篇关于使用 UIPageViewController 时如何将 UIBarButtonItem 添加到 NavigationBar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:UINavigationController每一页的UIToolbar 下一篇:如何在推送的视图控制器下方弹出视图控制器?

                相关文章

                <small id='j0Ik3'></small><noframes id='j0Ik3'>

              • <legend id='j0Ik3'><style id='j0Ik3'><dir id='j0Ik3'><q id='j0Ik3'></q></dir></style></legend>
                • <bdo id='j0Ik3'></bdo><ul id='j0Ik3'></ul>
              • <tfoot id='j0Ik3'></tfoot>

                  <i id='j0Ik3'><tr id='j0Ik3'><dt id='j0Ik3'><q id='j0Ik3'><span id='j0Ik3'><b id='j0Ik3'><form id='j0Ik3'><ins id='j0Ik3'></ins><ul id='j0Ik3'></ul><sub id='j0Ik3'></sub></form><legend id='j0Ik3'></legend><bdo id='j0Ik3'><pre id='j0Ik3'><center id='j0Ik3'></center></pre></bdo></b><th id='j0Ik3'></th></span></q></dt></tr></i><div id='j0Ik3'><tfoot id='j0Ik3'></tfoot><dl id='j0Ik3'><fieldset id='j0Ik3'></fieldset></dl></div>