将相同的 UIBarButtonItem 添加到多个 UIViewControllers [英] Add the same UIBarButtonItem to several UIViewControllers

查看:29
本文介绍了将相同的 UIBarButtonItem 添加到多个 UIViewControllers的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将相同的 UIBarButtonItem 添加到多个 UIViewController 中.我以前通过创建一个父 UIViewController 来完成此操作,该父UIViewController 设法在 viewDidLoad 中添加 UIBarButtonItem.但是现在我有许多从不同的视图控制器继承的视图控制器,所以如果我遵循相同的策略,我应该为每个不同的视图控制器创建一个 UIViewController 子类.

I want to add the same UIBarButtonItem to several UIViewControllers. I've done this previously by creating a parent UIViewController that manages to add the UIBarButtonItems in viewDidLoad. But now I have many view controllers that inherit from different view controllers, so if I follow the same strategy I should create a UIViewController subclass for each one of the different view controllers.

我实际上在做的是在我的 BaseViewController 中配置导航栏按钮,其余所有的父控制器,取决于我在创建子视图控制器时设置的标志.使用此解决方案,代码仅依赖于一个视图控制器,但我希望是否有更优雅的选项来实现这一点.

What I am actually doing is to configure the navigation bar buttons in my BaseViewController, the parent controller of all of the rest, depending on the flags I set when creating the child view controllers. With this solution the code only relies on one view controller, but I would like if there are more elegant options to achieve this.

推荐答案

解决方案之一是使用objective-c 类别.在这种情况下,您将为所有 UIViewController 创建通用方法,该方法将创建和配置 UIBarButton

One of solutions is to use objective-c category. In this case, you will create common method for all UIViewControllers, that will create and configure UIBarButton

这篇关于将相同的 UIBarButtonItem 添加到多个 UIViewControllers的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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