2011-08-08 52 views
1

我有3個視圖控制器(VC)AB C.首先我呈現A.然後我推B,然後推C.當我推C後,我從堆棧中刪除B,所以如果用戶按下返回鍵。我用這個代碼推C和刪除B:Uinavigationitem後退按鈕不可見,直到我點擊導航的左側。 bar

UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"Nazaj" style:UIBarButtonItemStylePlain target:nil action:nil]; 

    //we push C 
    PorabaControllerR *anotherViewController = [[PorabaControllerR alloc] initWithNibName:@"PorabaViewR" bundle:nil]; 
    //[anotherViewController setTitle:@"Pregled porabe"]; 
    [anotherViewController.navigationItem setBackBarButtonItem:backButton]; 
    [self.navigationController pushViewController:anotherViewController animated:YES]; 
    [anotherViewController release]; 

    //we remove B from the stack 
    NSMutableArray *allControllers = [[NSMutableArray alloc] initWithArray:self.navigationController.viewControllers]; 

    [allControllers removeObjectAtIndex:[allControllers count] - 2]; 
    //[allControllers objectAtIndex:[allControllers count] - 2] 
    [self.navigationController setViewControllers:allControllers animated:NO]; 
    [allControllers release]; 

問題是,uinav。項目的後退按鈕不會顯示在C上,直到我走到它上面並點擊它。 (在B上是OK)。 如何在執行過程中調試它或者觀看按鈕標題更改有沒有什麼好方法? 有沒有其他想法?

編輯:我試着用維傑的想法:

UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"Nazaj" style:UIBarButtonItemStylePlain target:self action:@selector(leftBarButtonClick:)]; 

-(IBAction)leftBarButtonClick:(UIButton *) sender { 

NSLog(@"clicked left"); 
//back to home screen 
[self.navigationController popToRootViewControllerAnimated:YES]; 

}

,但此功能未在所有打來電話,後退按鈕仍然隱藏,直到我走了過來,並點擊它。

回答

0

我這個代碼解決它裏面:

UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"Nazaj" style:UIBarButtonItemStylePlain target:nil action:nil]; 

    PorabaControllerR *anotherViewController = [[PorabaControllerR alloc] initWithNibName:@"PorabaViewR" bundle:nil]; 
    [anotherViewController.navigationItem setBackBarButtonItem:backButton]; 

    NSMutableArray *allControllers = [[NSMutableArray alloc] initWithArray:self.navigationController.viewControllers]; 

    [allControllers removeObjectAtIndex:1]; //we remove B 
    [allControllers insertObject:anotherViewController atIndex:1]; //we push C 
    [self.navigationController setViewControllers:allControllers animated:YES]; 
    [allControllers release]; 
0

只需在你的backbarbutton或leftbarbutton動作中使用此代碼即可。

烏爾功能

[self.navigationController popToRootViewControllerAnimated:YES]; 

OR

[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:0] animated:YES]; 
相關問題