2014-06-29 41 views
0

我的應用程序是一個帶有許多導航控制器的標籤欄應用程序。 我希望這些導航控制器能夠通過自定義右鍵欄按鈕來處理登錄/註銷操作。所以我成立的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 
+0

您可以添加'@ interface'的'請連接導航控制器? – lucianomarisi

+0

@lucianomarisi我做到了,你會發現裏面沒有很多東西。 – Rob

回答

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 

作爲一個說明,如果你在你的CustomNavigationControllerpo self.viewControllers將斷點您displayConnectionButton我想你會得到一個空數組(即沒有視圖控制器)

+0

感謝您的回答。這是我第一次做的,但如果我這樣做,我將不得不在我的應用程序的每個控制器中添加相同的代碼,對吧? – Rob

+0

你可以用這個代碼創建一個'BaseViewController'類,並且從你的'MyFirstViewController'和'MySecondViewController'子類。 – lucianomarisi

+0

這是正確的。 UINavigationBar(在您的UINavigationController中)管理從您的所有UIViewControllers獲取的UINavigationItems堆棧。有關更多信息,請閱讀navigationItem的UIViewController doc條目。 – Brandon