2013-04-18 62 views
29

我有一個UIView->UICollectionView->UICollectionViewCell。我試圖以編程方式返回,但這些都不起作用。代碼確實叫。 我正在使用StoryBoard。導航返回上一個視圖控制器

- (void) goBack:(NSNotification *) notification { 
     // [self.navigationController popViewControllerAnimated:YES]; 
    // [self dismissViewControllerAnimated:YES completion:nil]; 
     [self.navigationController popToRootViewControllerAnimated:YES]; 
} 
+0

讓我看看你的按鈕動作代碼或通知代碼? – Balu 2013-04-18 06:02:21

+0

你是否在你的類中獲得self.navigationControlle而不是零?檢查這個。 – 2013-04-18 06:06:40

+0

[[NSNotificationCenter defaultCenter] postNotificationName:@「goBack」object:nil]; – user1688346 2013-04-18 06:08:28

回答

83

您需要使用:

[self.navigationController popToRootViewControllerAnimated:YES]; 

這將帶你回到根視圖控制器。

如果要導航回到以前的視圖控制器,你應該實現:

[self.navigationController popViewControllerAnimated:YES]; 
+0

此代碼不適用於我 – 2017-05-08 11:20:37

+0

您是否將UINavigationController設置爲您的rootViewController?如果沒有,那麼它將無法工作。 – 2017-05-09 06:26:58

0
- (void) goBack:(NSNotification *) notification 
{ 
    if(!self.YOrView.isHidden) 
     self.YOrView.hidden = YES; 
} 
1

試試吧....

#import "bookdescriViewController.h" // import here your class name 

- (IBAction)backButton:(id)sender 
{ 
    bookdescriViewController *previosVC = [[bookdescriViewController alloc]init]; 
    [self.navigationController popViewControllerAnimated:YES]; // go to previous view controller 
    [self.navigationController popToRootViewControllerAnimated:YES]; // go to root view controller 
    [self.navigationController popToViewController:previosVC animated:YES]; // go to any view controller 
    [previosVC release]; 
} 
5

......怎麼

[self.navigationController dismissViewControllerAnimated:YES completion:NULL]; 

假設您目前在一個基於導航控制器和你想在進入基於導航的控制器之前,請回到前一個控制器。

17

通過使用以下行,我們可以去父視圖控制器

[self.navigationController popViewControllerAnimated:YES]; 

通過使用下面一行,我們可以移動到主/根視圖控制器

[self.navigationController popToRootViewControllerAnimated:YES]; 

通過使用下面的行,我們可以移動到任何視圖控制器

[self.navigationController popToViewController:viewControllerObject animated:YES]; 
爲便於複製粘貼
3

斯威夫特解決方案:

navigationController?.popViewControllerAnimated(true) 
0

回到父視圖控制器的dealloc當前視圖控制器EX:

- (void)applicationDidEnterBackground:(NSNotification *)notification 
{ 
    NSInteger numberOfViewControllers = self.navigationController.viewControllers.count; 

    UIViewController *vc = [self.navigationController.viewControllers objectAtIndex:numberOfViewControllers - 2]; 

    [self.navigationController popToViewController:vc animated:NO]; 
} 

或其他視圖控制器

1

With swift3,

@IBAction func back(_ sender: UIButton) { 
    self.dismiss(animated: true, completion: nil)  
} 
相關問題