Navigate Back to previous view controller

2019-02-01 10:26发布

I have a UIView->UICollectionView->UICollectionViewCell. I am trying to navigate back programatically but none of these works. The code did called. I am using StoryBoard.

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

9条回答
【Aperson】
2楼-- · 2019-02-01 11:01

Go back to parent view controller and dealloc current view controller 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];
}

or another view controller

查看更多
疯言疯语
3楼-- · 2019-02-01 11:09

With swift3,

@IBAction func back(_ sender: UIButton) {
    self.dismiss(animated: true, completion: nil)     
}
查看更多
相关推荐>>
4楼-- · 2019-02-01 11:12

By using below line we can go to parent view controller

[self.navigationController popViewControllerAnimated:YES]; 

By using below line we can move to main/root view controller

[self.navigationController popToRootViewControllerAnimated:YES]; 

By using below line we can move to any view controller

[self.navigationController popToViewController:viewControllerObject animated:YES]; 
查看更多
ら.Afraid
5楼-- · 2019-02-01 11:13

Swift solutions for easy copy pasting:

navigationController?.popViewControllerAnimated(true)
查看更多
Rolldiameter
6楼-- · 2019-02-01 11:16

How about...

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

Assuming that you are currently in a navigation-based controller and you wanted to go back to the previous controller before you got into a navigation-based controller.

查看更多
啃猪蹄的小仙女
7楼-- · 2019-02-01 11:16

Swift 4.1:

navigationController.popViewController(animated: true)
查看更多
登录 后发表回答