2013-05-13 84 views
0

如果用戶點擊我創建的後退按鈕,我的回答視圖控制器(顯示答案的視圖控制器)上有一個後退按鈕切換到視圖的標題爲「返回」,只是一個空的桌面視圖,然後切換回我的主要視圖,顯示所有需要回答的問題。這是爲什麼發生?它是一個非常簡短的事情,但絕對明顯!導航欄按鈕在加載相應視圖之前加載隨機視圖

UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 48)]; 
navBar.delegate = self; 
UINavigationItem *backItem = [[UINavigationItem alloc] initWithTitle:@"Back"]; 
[navBar pushNavigationItem:backItem animated:NO]; 

UINavigationItem *topItem = [[UINavigationItem alloc] initWithTitle:@"Question"]; 
[navBar pushNavigationItem:topItem animated:NO]; 
topItem.leftBarButtonItem = nil; 

[self.view addSubview:navBar]; 


- (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPopItem:(UINavigationItem *)item 
{ 

    ViewController *controller = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil]; 
    controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal; 
    [self presentViewController:controller animated:YES completion:nil]; 
    return true; 
} 

- (void)navigationBar:(UINavigationBar *)navigationBar didPopItem:(UINavigationItem *)item 
{ 

    ViewController *controller = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil]; 
    controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal; 
    [self presentViewController:controller animated:YES completion:nil]; 
} 

Picture of view with back button Picture of problem that is occurring

+1

爲什麼你自己管理導航欄而不是使用導航控制器?您正在創建一個標題爲「返回」的導航項... – Wain 2013-05-13 22:24:52

+0

你是什麼意思? – heinst 2013-05-13 22:31:35

回答

2

你創建自己的UINavigationBarUINavigationItem情況下,你可能不應該。您所描述的情況正是UINavigationController的用途。當使用UINavigationController時,它會創建您在屏幕上顯示的UINavigationBar和每個UIViewController(推入導航控制器)都有其自己的UINavigationItem(標題取自視圖控制器的title)。

你題爲「回到」空「觀點」的原因是,你創造它:

UINavigationItem *backItem = [[UINavigationItem alloc] initWithTitle:@"Back"]; 
[navBar pushNavigationItem:backItem animated:NO]; 

省卻了這一切,創造UINavigationController,使你的問題的視圖控制器它的根視圖控制器,然後將導航控制器添加到屏幕。然後,當回答問題時,請按下回答視圖控制器:

[self.navigationController pushViewController:answerViewController animated:YES]; 
+1

好的答案,我只是寫了同樣的東西,但你打敗了我。 – zimmryan 2013-05-13 22:48:35