2012-06-21 69 views
1

我該如何去有關UINavigation控制器不導航到前一個視圖,但之前的視圖。基本上我希望它跳回2個位置,而不是默認的位置。如何獲得一個uinavigation欄返回按鈕返回到anthother視圖控制器

這是非常規的,我敢肯定,但我只需要現在就做。

self.navigationItem.backBarButtonItem = 
    [[[UIBarButtonItem alloc] initWithTitle:@"Back" 
             style:UIBarButtonItemStyleBordered 
            target:nil 
            action:nil] autorelease]; 

感謝您的任何幫助。

回答

4

設置按鈕:

self.navigationItem.backBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStyleBordered target:self action:@selector(goBack)] autorelease]; 

然後創建一個方法-goBack

-(void)goBack 
{ 
    UIViewController *ctrl = [self.navigationController.viewControllers objectAtIndex:self.navigationController.viewControllers.count - 2]; 
    [self.navigationController popToViewController:ctrl animated:YES]; 
} 
+0

謝謝!如果我想指定一個特定的視圖控制器,該怎麼辦?這有可能沒有打破導航流? – hanumanDev

1

目標添加到您添加

然後使用下面的代碼返回大於1的viewController

//Get the view controller that is 2 step behind 
UIViewController *controller = [nav.viewControllers objectAtIndex:nav.viewControllers.count - 2]; 

//Go to that controller 
[nav popToViewController:controller animated:YES]; 
1
self.navigationItem.backBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStyleBordered target:self action:@selector(youractonEvent:] autorelease]; 
1

如果你不想依賴於數組的索引,那麼你可以做一些事象下面這樣:

MyController * aMyController = nil ; 
for (int i=0; i<[[self.navigationController viewControllers] count]; i++) 
{ 
    NSLog(@"%@",[[self.navigationController viewControllers] objectAtIndex:i]); 
    if ([[[self.navigationController viewControllers] objectAtIndex:i] isKindOfClass:[MyController class]]) 
    { 
     aMyController = [[self.navigationController viewControllers] objectAtIndex:i];  
    } 
}   
[self.navigationController popToViewController:aMyController animated:YES]; 
1

如果您有NavigationController三個視圖控制器和當前正在查看3日視圖控制器,如果你想要跳轉到第1個視圖控制器。

試試這個。這將導航你兩次回來。

[self.navigationController popToViewController:[[self.navigationController viewControllers] objectAtIndex:0] animated:YES]; 

在你的情況下設置合適的值而不是0。

相關問題