简体   繁体   English

为左键设置重复使用的导航栏项目按钮

[英]Setting up reused navigation bar item button for left button

I am using a Navigation Bar through out my application although not controller via a Navigation Controller (added to the VCs via Storyboard). 我在整个应用程序中都使用了导航栏,尽管不是通过导航控制器(通过情节提要添加到VC)中的控制器。

I would like to set a left bar button item to be the same on the navigation controller through out its use. 我想在整个使用过程中在导航控制器上将一个左栏按钮项设置为相同。 This is a stack style for the menu which will slide in: 这是菜单的堆栈样式,将在其中滑动:

在此处输入图片说明 What is the correct way to implement this button following standard practice? 按照标准做法,实现此按钮的正确方法是什么? I can strip the bg of the button to be repeatable which is not an issue, but how do I complete the following: 1. Implement an image within the button 2. Implement this button through appearance API if possible so the code is not glue code and re-used on all VCs that have this. 我可以将按钮的bg剥离为可重复的,这不是问题,但是如何完成以下操作:1.在按钮内实现图像2.如果可能的话,通过外观API实现此按钮,因此代码不是粘合代码并在具有此功能的所有VC上重复使用。

If I cannot do via appearance, what would be the best approach to not have to include this code over and over again in the controllers that use this? 如果我不能通过外观做到这一点,最好的方法是不必在使用此代码的控制器中一遍又一遍地包含此代码?

Further to this, is there a way to show this button item in a storyboard and be linked in the storyboard/have a use other than visually. 除此之外,还有一种方法可以在情节提要中显示此按钮项,并在情节提要中进行链接/除了视觉上有用途。

Ah ok, now I'm following you. 好的,现在我正在追踪您。 In that case just create a custom navBar class and either add that to your pch then use it where necessary or create a custom UIViewController subclass called "UIViewControllerWithNavBar" then you can use that class as a replacement for the default UIViewController class. 在这种情况下,只需创建一个自定义的navBar类,然后将其添加到您的pch中,然后在必要时使用它,或者创建一个名为“ UIViewControllerWithNavBar”的自定义UIViewController子类,则可以使用该类替代默认的UIViewController类。

@interface SomeViewController : UIViewControllerWithNavBar

This will allow you to reuse the class throughout your app with the navBar already added. 这将允许您在已经添加了navBar的情况下在整个应用程序中重用该类。

I recently wrote a blog post about this issue, but for the rightBarButtonItem. 我最近写了一篇有关此问题的博客文章,但写的是rightBarButtonItem。 I'm not sure if the "back" functionality will require tweaks, but here's the blog post if interested: http://www.codebestowed.com/ios-shared-barbuttonitems/ 我不确定“后退”功能是否需要调整,但是如果有兴趣, 请参见以下博客文章: http : //www.codebestowed.com/ios-shared-barbuttonitems/

The basic idea is to subclass UINavigationController, give it a BarButtonItem property (self.myButton in the code below) and add code similar to: 基本思想是继承UINavigationController的子类,为其赋予BarButtonItem属性(在下面的代码中为self.myButton),并添加类似于以下代码:

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.delegate = self;
}

- (void)navigationController:(UINavigationController *)navigationController
    willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    if (!viewController.navigationItem.rightBarButtonItem) {
        viewController.navigationItem.rightBarButtonItem = self.myButton;
    }
}

The blog post details how you can set this up further in InterfaceBuilder, which requires a bit of a hack (too much to go into in this answer). 博客文章详细介绍了如何在InterfaceBuilder中进行进一步设置,这需要一点技巧(此答案太多了)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM