2013-09-05 32 views
1

我在我的項目中使用FlatUIKit。我有這樣定義的自定義後退按鈕:iOS:自定義後退按鈕不起作用

[UIBarButtonItem configureFlatButtonsWithColor:[UIColor peterRiverColor] 
           highlightedColor:[UIColor belizeHoleColor] 
            cornerRadius:3 
           whenContainedIn:[UINavigationBar class], nil]; 

    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Back" 
                       style:UIBarButtonItemStylePlain 
                      target:self 
                      action:@selector(clickBack)]; 

這裏是我的點擊返回

-(void)clickBack{ 
[self.navigationController popToViewController:[self.navigationController.viewControllers  objectAtIndex:self.navigationController.viewControllers.count-1] animated:YES]; 

} 

,因爲我希望它出現的按鈕,但僅此而已。如果我點擊它,什麼都不會發生。

任何想法我做錯了什麼?我對此很新,並且從未替換過後退按鈕。

+2

'objectAtIndex:計數1'將會返回當前視圖控制器,而不是以前的。 –

+1

您是否嘗試在clickBack方法內設置斷點?你的代碼到達那個部分了嗎?或者在那裏寫一個NSLog,看看是否打印... – Emilio

+0

謝謝邁克。我試圖將它設置爲-2,但它使應用程序崩潰。 – user2747220

回答

2

您剛纔在您的評論中提到您以模態方式呈現視圖控制器,這是不同的。你會想釋放模態視圖控制器。試試這個:

[self dismissModalViewControllerAnimated:YES]; 

pop方法只會如果你正在釋放視圖被推到導航控制器開始工作。

下面是模態視圖控制器示例的鏈接。

http://timneill.net/2010/09/modal-view-controller-example-part-1/

+0

Aww,是的,現在有效。傻我。非常感謝。 – user2747220

1

使用這個代碼在點擊返回方法

[self.navigationController popViewControllerAnimated:YES]; 
+0

謝謝,我已經嘗試過,但實際上根本不起作用。 – user2747220

+0

這聽起來像你實際上沒有以前的視圖控制器回去。 –

+0

我的確如此。是因爲我模態地引入了新的視圖控制器嗎?如果我選擇推送,應用程序就會崩潰。 – user2747220

0
[UIBarButtonItem configureFlatButtonsWithColor:[UIColor peterRiverColor] 
           highlightedColor:[UIColor belizeHoleColor] 
            cornerRadius:3 
           whenContainedIn:[UINavigationBar class], nil]; 

    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Back" 
                       style:UIBarButtonItemStylePlain 
                      target:self 
                      action:@selector(clickBack)]; 


-(void)clickBack 
{ 

[self.navigationController popToRootViewControllerAnimated:YES]; 

} 

副本,並因爲它是粘貼此代碼。