2013-03-28 102 views
3

我試圖創建一個視圖控制器,並設置其視圖和標題欄。事情似乎工作得很好。但是,當我嘗試設置導航欄的左側按鈕或右側按鈕時,他們只是不顯示。我究竟做錯了什麼?添加按鈕不顯示在UINavigationController上

UIWebView *tempWebView = [[UIWebView alloc] initWithFrame:self.view.bounds]; 
    [tempWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"someurl"]]]; 

    UIViewController* tempViewController = [[UIViewController alloc]init]; 
    tempViewController.view = tempWebView; 

    UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:tempViewController]; 

    UIBarButtonItem* closeButton = [[UIBarButtonItem alloc] initWithTitle:@"Close" style:UIBarButtonItemStyleBordered target:self action:nil]; 
    navigationController.navigationItem.leftBarButtonItem = closeButton; 


    [self presentViewController:navigationController animated:YES completion:nil]; 

回答

7

變化

navigationController.navigationItem.leftBarButtonItem = closeButton; 

tempViewController.navigationItem.leftBarButtonItem = closeButton; 

navigationItem外觀由正由導航控制器呈現在觀察處理,所以如果你把它放在了UINavigationController但不是在rootViewController ,當你第一次推它的時候會被覆蓋。

+0

這有助於我使用ParentViewController設置左鍵按鈕 – Irwin

1

您需要訪問tempViewController的navigationItem,而不是navigationController。

相關問題