使用导航栏后退按钮返回主视图

时间:2022-02-02 21:44:32

My app has a main view with a few buttons lined up vertically. Each button is linked to a different view controller. Now, when the user taps on the first button, the first view controller is opened. Within the first view controller, there is a button which takes you directly to the second view controller. When I get to the second view controller, and press the back button, I am taken back to the first view controller. Then I have to press the back button one more time to be taken to the main view controller. What can I do to make the app take me directly to the main view controller from the second view controller, and not through the first view controller? Thanks in advance!

我的应用程序有一个主视图,几个按钮垂直排列。每个按钮都链接到不同的视图控制器。现在,当用户点击第一个按钮时,将打开第一个视图控制器。在第一个视图控制器中,有一个按钮可以直接将您带到第二个视图控制器。当我到达第二个视图控制器,然后按后退按钮时,我将被带回第一个视图控制器。然后我必须再次按下后退按钮才能进入主视图控制器。我该怎么做才能让应用程序从第二个视图控制器直接转到主视图控制器,而不是通过第一个视图控制器?提前致谢!

4 个解决方案

#1


0  

You could bypass the segue_unwind to go back to the main view controller. Just call the performSegueWithIdentifier in the IBAction of the button

您可以绕过segue_unwind返回主视图控制器。只需在按钮的IBAction中调用performSegueWithIdentifier即可

Swift

self.performSegueWithIdentifier("SegueIdentifierName", sender: self)

Objective C

[self performSegueWithIdentifier:@"SegueIdentifierName" sender:self];

Here is a similar article as well What are Unwind segues for and how do you use them?

这里有一篇类似的文章,Unwind segues是什么以及如何使用它们?

#2


0  

You can override the back button action to call popToViewController() on the navigationController with the view controller you would like to return to.

您可以覆盖后退按钮操作,以使用您想要返回的视图控制器调用navigationController上的popToViewController()。

#3


0  

There are a lot of answers in SO about this BUT:

SO中有很多关于此的答案但是:

[self.navigationController popToRootViewControllerAnimated:YES];

OR

要么

UIViewController *rootViewController = [self.navigationController.viewControllers firstObject];
[self.navigationController pushViewController:rootViewController animated:YES];

Should work just fine.

应该工作得很好。

Next time, TRY to find answers first: How do I get the RootViewController from a pushed controller?

下次,首先找到答案:如何从推送的控制器中获取RootViewController?

#4


0  

SWIFT 3.01

SWIFT 3.01

 self.performSegue(withIdentifier: "SegueIdentifierName", sender: self)

#1


0  

You could bypass the segue_unwind to go back to the main view controller. Just call the performSegueWithIdentifier in the IBAction of the button

您可以绕过segue_unwind返回主视图控制器。只需在按钮的IBAction中调用performSegueWithIdentifier即可

Swift

self.performSegueWithIdentifier("SegueIdentifierName", sender: self)

Objective C

[self performSegueWithIdentifier:@"SegueIdentifierName" sender:self];

Here is a similar article as well What are Unwind segues for and how do you use them?

这里有一篇类似的文章,Unwind segues是什么以及如何使用它们?

#2


0  

You can override the back button action to call popToViewController() on the navigationController with the view controller you would like to return to.

您可以覆盖后退按钮操作,以使用您想要返回的视图控制器调用navigationController上的popToViewController()。

#3


0  

There are a lot of answers in SO about this BUT:

SO中有很多关于此的答案但是:

[self.navigationController popToRootViewControllerAnimated:YES];

OR

要么

UIViewController *rootViewController = [self.navigationController.viewControllers firstObject];
[self.navigationController pushViewController:rootViewController animated:YES];

Should work just fine.

应该工作得很好。

Next time, TRY to find answers first: How do I get the RootViewController from a pushed controller?

下次,首先找到答案:如何从推送的控制器中获取RootViewController?

#4


0  

SWIFT 3.01

SWIFT 3.01

 self.performSegue(withIdentifier: "SegueIdentifierName", sender: self)