右键无法在自定义导航控制器中显示

时间:2014-06-29 14:26:38

标签: ios objective-c uinavigationcontroller

我的应用是带有许多导航控制器的标签栏应用。 我希望这些导航控制器使用自定义右键控制按钮来处理登录/注销操作。所以我在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

我尝试使用viewDidLoadviewDidAppearviewWillAppear但没有任何效果,我无法在导航栏中显示此按钮。我还尝试添加reloadInputViewssetNeedsDisplay。我该怎么办?

感谢您的帮助

编辑:CustomNavigationController的界面

@interface ConnectionNavigationController : UINavigationController

- (void) displayConnectionButton;

@end

1 个答案:

答案 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

请注意,如果您在displayConnectionButtonCustomNavigationController po self.viewControllers上放置断点,我认为您将获得一个空数组(即没有视图控制器)