1

我創建了一個基於窗口的新應用程序,以及一個用於sublcasses UIViewController的RootViewController。我在AppDelegate中創建了一個UINavigationController,當我將UINavigationController的視圖添加到窗口中時,我確實在頂部看到了導航欄。但是,我似乎無法在導航欄中添加按鈕 - 沒有編譯錯誤,但按鈕(和標題)不顯示。代碼如下:BarButtonItem不顯示

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 

    RootViewController * rootViewController = [[RootViewController alloc] initWithNibName:@"RootViewController" bundle:nil]; 
    UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:rootViewController]; 

    UIBarButtonItem *next = [[UIBarButtonItem alloc] initWithTitle:@"Next" style:UIBarButtonItemStyleDone target:nil action:nil]; 
    navigationController.title = @"Foo"; 

    navigationController.navigationItem.rightBarButtonItem = next; 

    [self.window addSubview:navigationController.view]; 

    // Override point for customization after application launch. 
    [self.window makeKeyAndVisible]; 
    return YES; 


} 

我在做什麼錯?

+0

你嘗試設置在UIViewController子類的標題和rightBarButton呢?在我看來,'-viewDidLoad'似乎是一個合適的地方。還要確保UINavigationController不在你的UIViewController子類的'-viewDidLoad'裏面。 –

回答

0

更改此:

navigationController.navigationItem.rightBarButtonItem = next; 

這樣:

rootViewController.navigationItem.rightBarButtonItem = next; 
0

可能是樣式:僅在編輯模式下使用的UIBarButtonItemStyleDone爲YES。如果在根控制器或子視圖控制器中,您可以嘗試調用self.editing = YES,但在AppDelegate中則不行。