2013-04-18 64 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)  
} 
相关问题