xcode 如何使按钮返回上一个视图 X

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14612071/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 02:40:17  来源:igfitidea点击:

How to make at button to return to previous view X

xcodeios6

提问by user1607498

I might be starring blind. Im trying to create a button that returns the previous view. But, the catch is, that I can navigate to this view, from three different views and I want to make sure I return to the correct one. Im not goint to use a navigation controller for this - I′d like my button to handle this transistion.enter image description here

我可能是瞎主演。我正在尝试创建一个返回上一个视图的按钮。但是,问题是,我可以从三个不同的视图导航到该视图,并且我想确保返回到正确的视图。我不打算为此使用导航控制器 - 我希望我的按钮来处理这个转换。在此处输入图片说明

I have illutstrated what Im trying to do. When I navigate to 'View1' from either view A, B or C, I want the same one button to be able to return me to the view I came from.

我已经说明了我想要做什么。当我从视图 A、B 或 C 导航到“View1”时,我希望同一个按钮能够将我返回到我来自的视图。

Is this possible?

这可能吗?

Kind regards

亲切的问候

The new-dev-guy

新开发人员

回答by The iOSDev

The other way which I preferred is to use the UINavigationController. From the above diagram what I can understand is the from view A, view B, View Cyou can navigate to the View 1and then when you want to go back just click or tap on back button will take you the the previous controller.
For that just use the [self.navigationController popViewControllerAnimated:YES];for navigate to the previous view controller with animation an replace NO instead of YES to navigate to the previous view without animation.
And for navigate to the View 1use the [self.navigationController pushViewController:View 1 animated:YES];for animated navigation and for non-animated transition use NO.

我更喜欢的另一种方式是使用UINavigationController. 从上图中我可以理解的是,从视图 A视图 B视图 C您可以导航到视图 1,然后当您想返回时,只需单击或点击后退按钮即可将您带到前一个控制器。
为此,只需使用[self.navigationController popViewControllerAnimated:YES];for 导航到带有动画的前一个视图控制器,替换 NO 而不是 YES 以导航到没有动画的前一个视图。
对于导航到视图 1使用[self.navigationController pushViewController:View 1 animated:YES];动画导航和非动画过渡使用 NO。

回答by Dhruv Goel

Not sure why you want to do it like this, but if you absolutely have to, then do this: Assuming you have all the 4 views and the back button in a single controller, create a UIVIew property called "previousView" in your controller, and just before animating View1 from either View A, B or C, just set self.previousView = viewA(or B or C), now on pressing the back button, you'll know which view to go back to from the saved previousView property.

不确定为什么要这样做,但如果绝对必须这样做,请执行以下操作:假设您在单个控制器中拥有所有 4 个视图和后退按钮,请在控制器中创建一个名为“ previousView”的 UIVIew 属性,就在从视图 A、B 或 C 为 View1 设置动画之前,只需设置self.previousView = viewA(或 B 或 C),现在按下后退按钮,您将知道从保存的 previousView 属性返回哪个视图.