如何在使用UIPageViewController时将UIBarButtonItem添加到NavigationBar [英] How to add UIBarButtonItem to NavigationBar while using UIPageViewController
问题描述
请原谅,关于如何以编程方式将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.
我让页面视图控制器与三个独特的viewController一起很好地工作.我现在想为每个视图控制器设置唯一的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;
我知道我已经接近这里了,我只是不确定如何指定我需要向运行我的contentViewController嵌入的detailViewController的NavigationController的NavigationBar中添加一个按钮.任何帮助都特别重要片段.
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 thanviewDidAppear
- This will not work because the
viewDidLoad
method is not called everytime you swipe from one page to the next. TheviewDidAppear
method is however so I am trying to set the button there. It is possibleviewWillAppear
is called everytime but I haven't checked.
- 如果用户快速翻阅页面,则按钮将无法更改或无法显示,这是有问题的.
解决方案
SOLUTION
事实证明我已经关闭了……而不是在UIPageViewController方法中创建按钮,我只需要在我的三个视图控制器中的每一个中创建一个
navigationItem
属性.当在委托方法中实例化控制器时,我只需将navigationItem属性设置为与detailViewController相等.这样,在我的viewDidApper
方法中,我可以创建按钮.然后在viewWillDisappear
处将按钮设置为nil
.您也可以只在DetailViewController
的viewdidLoad
处创建按钮,并在各个viewController的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 myviewDidApper
methods I could create the button. Then atviewWillDisappear
I set the button tonil
. You can also just create the button atviewdidLoad
of theDetailViewController
and change the text and action inviewDidAppear
of the individual viewControllers. Here is a sample.在委托方法中
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController - (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
我根据viewController的索引调用一个辅助方法.辅助方法在调用时实例化视图控制器.实例化后,我将设置一些属性,包括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 callsetTitle:forState
withUIControlStateNormal
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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- This will not work because the
- 这将不起作用,因为您每次从一页滑至下一页时都不会调用