将按钮添加到NavigationBar的标题视图,而无需重复代码 [英] Adding buttons to the titleview of NavigationBar without having to repeat code

查看:132
本文介绍了将按钮添加到NavigationBar的标题视图,而无需重复代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了给出一些背景知识,我正在制作一个基于UINavigationControlled的博客类型应用程序(我认为它最像iPhone Facebook应用程序)。无论当前处于活动状态的视图如何,NavigationBar都会在其标题区域中显示一些按钮,例如朋友请求和活动通知。与Facebook应用程序大致相同,单击这些按钮将创建一个弹出视图。

To give some background, I am making a UINavigationControlled-based blog type app (I suppose it most closely resembles the iPhone facebook app). Regardless of the view that is currently active, the NavigationBar shows some buttons in its title area, such as Friend Requests and Activity Notifications. In much the same was as the facebook app, clicking these buttons will create a popup view.

目前,我正在以一种我觉得非常低效的方式做事。对于每个加载的视图,我正在重新创建按钮并将它们添加到导航栏。我的代码如下:

Currently, I am doing things in a way that I feel is incredibly inefficient. For every view that is loaded, I am recreating the buttons and adding them to the navigation bar. My code is below:

//Setup the custom middle buttons
UIView *container = [[UIView alloc] init];
container.frame = CGRectMake(0, 0, 80, 44);

// create a button and add it to the container
UIButton *notificationButton = [UIButton buttonWithType:UIButtonTypeCustom];
notificationButton.frame = CGRectMake(0, 0, 35, 44);
[notificationButton addTarget:self 
                       action:@selector(showNotifications:) 
             forControlEvents:UIControlEventTouchUpInside];
[container addSubview:notificationButton];


// add another button to the container
UIButton *friendActivityButton = [UIButton buttonWithType:UIButtonTypeCustom];
friendActivityButton.frame = CGRectMake(45, 0, 35, 44);
[friendActivityButton addTarget:self 
                         action:@selector(showFriendActivity:) 
               forControlEvents:UIControlEventTouchUpInside];
[container addSubview:friendActivityButton];

// Set the titleView to the container view
[self.navigationItem setTitleView:container];
[container release];

由于我的应用有多个视图且导航栏始终可见,因此继续重新创建似乎很愚蠢按钮,将它们添加到容器视图,然后将该视图添加到导航控制器的titleView。

Since my app has multiple views and the navigation bar is always visible, it seems silly to keep on recreating the buttons, adding them to a container view, then adding that view to the titleView of the navigation controller.

实现相同效果的更好方法是什么?我正在研究子类化或为UINavigationBar创建一个类别,并可能在那里添加容器视图代码。但是,我不知道如何让选择器在这些情况下工作。我也不确定如何使用UINavigationBar类别访问titleView属性。

What would be a better way of achieving the same effect? I was looking into subclassing or creating a category for UINavigationBar and maybe adding in the container view code there. However, I did not know how to make the selectors work in these cases. I also wasnt sure how to get access to the titleView property with a UINavigationBar category.

任何有关这方面的帮助都会很棒!谢谢!

Any help on this would be great! Thank you!

推荐答案

我建议将上面的代码放在 UIViewController category(或者 UIViewController 子类,如果你可以从一个根子类化所有视图控制器)。然后在 viewDidAppear 中,只需调用上面的代码(或将其放在超类的 viewDidAppear 中)。我认为继承 UINavigationBar 的子类将比它的价值更麻烦。

I'd recommend just putting the above code in a UIViewController category (or a UIViewController subclass if you can subclass all your view controllers from a single root). Then in viewDidAppear, just call the above code (or put that in the superclass's viewDidAppear). I think that subclassing UINavigationBar is going to be more trouble than it's worth.

如果你真的想避免重新创建按钮(可能没有理由这样做,但如果由于某些原因它对你很重要),你可以将按钮连接到一些单例对象,而不是将它们指向 self 。单例可以直接处理响应,将消息转发到活动视图控制器,也可以发布活动视图控制器可以监听的通知。

If you really want to avoid recreating the buttons (probably no reason to do so, but if it's important to you for some reason), you can wire the buttons to some singleton object rather than pointing them at self. The singleton could either handle the response directly, forward the message to the active view controller, or post a notification that the active view controller could listen for.

这篇关于将按钮添加到NavigationBar的标题视图,而无需重复代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆