我希望在整个应用程序中替换 UINavigationController 中的后退按钮。我的要求是这个后退按钮被定义在一个 XIB 中,如果可能的话,设置它的代码在一个地方。
我看过various methods设置属性 self.navigationItem.backBarButtonItem
成为具有自定义按钮的 UIBarButtomItem 作为它的 View ,例如[[UIBarButtonItem alloc] initWithCustomView:myButton];
我的第一个想法是创建一个全局类别(不确定这是否是术语,我是 Objective-C 的新手,您可能已经猜到了)为我的所有 UINavigationController 实现“ViewDidLoad”,并设置此属性。我的问题是将 XIB 加载到我在运行时创建的这个按钮。
有没有人建议以一种简洁的方式做到这一点(我想这一定是一件很常见的事情,我无法想象在我的所有屏幕上重复代码)。我考虑过创建一个 UINavigationController
子类,但是我不确定这将如何影响我的 ViewDidLoad 自定义实现。
非常感谢任何建议。我还需要定位 >= iOS4(外观 API 仅适用于 iOS5)。
最佳答案
我宁愿在可能的情况下不强制继承,所以你可以用两个类别来做到这一点
@interface UIViewController (backButtonAdditions)
- (void)ps_addBackbutton;
@end
@implementation UIViewController (backButtonAdditions)
- (void)ps_addBackbutton;
{
// add back button
}
@end
@interface UINavigationController (backButtonAdditions)
- (void)ps_pushViewController:(UIViewController *)viewController animated:(BOOL)animated;
@end
@implementation UINavigationController (backButtonAdditions)
- (void)ps_pushViewController:(UIViewController *)viewController animated:(BOOL)animated;
{
[viewController ps_addBackbutton];
[self pushViewController:viewController animated:animated];
}
@end
现在
#import
这些文件是适当的,而不是使用[self.navigationController pushViewController:aViewController YES];
采用
[self.navigationController ps_pushViewController:aViewController YES];
免责声明
我在浏览器中自由设置了这个样式,所以你可能需要调整它
关于objective-c - 整个应用程序中的自定义后退按钮 UINavigationController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10026663/