我的应用是带有许多导航控制器的标签栏应用。 我希望这些导航控制器使用自定义右键控制按钮来处理登录/注销操作。所以我在AppDelegate中以这种方式设置我的标签栏:
MyFirstViewController *firstViewController = [[MyFirstViewController alloc] init];
UIViewController *firstNavigationController = [[CustomNavigationController alloc]
initWithRootViewController:firstViewController];
MySecondViewController *secondViewController = [[MySecondViewController alloc] init];
UIViewController *secondNavigationController = [[CustomNavigationController alloc]
secondViewController]
initWithRootViewController:secondViewController];
....
[tabBarController setViewControllers:@[firstNavigationController, secondNavigationController]];
然后CustomNavigationController
:
@implementation ConnectionNavigationController
- (void) viewDidLoad
{
[self displayConnectionButton];
}
- (void) displayConnectionButton
{
UIImage *portraitImage, *landscapeImage;
portraitImage = [UIImage imageNamed:@"conn.png"];
landscapeImage = [UIImage imageNamed:@"connLandscape.png"];
UIBarButtonItem *connButton = [[UIBarButtonItem alloc]
initWithImage:[portraitImage
imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]
landscapeImagePhone:[landscapeImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]
style:UIBarButtonItemStylePlain target:self action:@selector(showConnectionPopup)];
self.navigationItem.rightBarButtonItem = connButton;
}
@end
我尝试使用viewDidLoad
,viewDidAppear
,viewWillAppear
但没有任何效果,我无法在导航栏中显示此按钮。我还尝试添加reloadInputViews
或setNeedsDisplay
。我该怎么办?
感谢您的帮助
编辑:CustomNavigationController的界面
@interface ConnectionNavigationController : UINavigationController
- (void) displayConnectionButton;
@end
答案 0 :(得分:0)
根据我的理解,您应该将displayConnectionButton
方法添加到相关的UIViewController
子类而不是UINavigationController
子类,因为它只包含视图控制器的堆栈。
E.g。
@implementation MyFirstViewController
- (void) viewDidLoad
{
[self displayConnectionButton];
}
- (void) displayConnectionButton
{
UIImage *portraitImage, *landscapeImage;
portraitImage = [UIImage imageNamed:@"conn.png"];
landscapeImage = [UIImage imageNamed:@"connLandscape.png"];
UIBarButtonItem *connButton = [[UIBarButtonItem alloc]
initWithImage:[portraitImage
imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]
landscapeImagePhone:[landscapeImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]
style:UIBarButtonItemStylePlain target:self action:@selector(showConnectionPopup)];
self.navigationItem.rightBarButtonItem = connButton;
}
@end
请注意,如果您在displayConnectionButton
和CustomNavigationController
po self.viewControllers
上放置断点,我认为您将获得一个空数组(即没有视图控制器)