My goal is to present a view controller without dismissing directly to its presentingViewController
.
我的目标是提供一个视图控制器,而不直接忽略它的presentViewController。
To make it more clear, consider the following example:
为了更清楚,请考虑以下示例:
Referring to this storyboard:
参考这个故事板:
Assuming that the first black view controller is the first (initial) one, it should present the third white view controller; The issue is the white view controller should dismisses to the second orange view controller but not the black one, so it should behave like:
假设第一个黑色视图控制器是第一个(初始)控制器,它应该呈现第三个白色视图控制器;问题是白色视图控制器应该关闭第二个橙色视图控制器而不是黑色视图控制器,所以它应该表现如下:
- Black VC Presents White VC.
- 黑色VC呈现白色VC。
- White VC Dismisses to Orange VC.
- 白色VC解散为Orange VC。
- Orange VC Dismisses to Black VC.
- Orange VC解散为黑色VC。
How to apply such a behavior?
如何应用这样的行为?
Remark: There is no navigation controller, it should be present/dismiss, not push/pop.
备注:没有导航控制器,它应该存在/解除,而不是推/弹。
5 个解决方案
#1
2
Here is one method...
这是一种方法......
- Black presents Orange
- 黑色呈现橙色
- In
viewDidLoad
, Orange instantiates White as a childVC and adds White's view as a 'full-screen' subview. - 在viewDidLoad中,Orange将White实例化为childVC,并将White的视图添加为“全屏”子视图。
- On button tap in White, White's view is animated away and removed from the hierarchy.
- 在白色按钮上,白色视图被动画化并从层次结构中删除。
- On button tap in Orange, Orange is dismissed, returning user to Black.
- 在橙色按钮上,橙色被解除,使用户返回黑色。
Here is method two...
这是方法二......
- Embed Black in a NavigationController without navigation bar
- 在没有导航栏的NavigationController中嵌入Black
- On tap in Black, instantiate Orange and White VCs
- 点击黑色,实例化橙色和白色VC
- Call
.setViewControllers:animated:
to "stack the views" on the navigation controller, and jump directly to the last view. - 调用.setViewControllers:animated:在导航控制器上“堆叠视图”,并直接跳转到最后一个视图。
- After that, navigation backwards uses the standard
.popViewController
functionality. - 之后,向后导航使用标准的.popViewController功能。
You can view a working example of both methods here: https://github.com/DonMag/SkipNavigation
您可以在此处查看这两种方法的工作示例:https://github.com/DonMag/SkipNavigation
#2
1
If the black VC presents both the orange and immediately the white, you'll have the structure you're looking for. You can turn off animations so that the orange would probably not be visible.
如果黑色VC同时显示橙色和白色,您将拥有您正在寻找的结构。您可以关闭动画,以便橙色可能不可见。
#3
1
This could be achieved by letting the first black view controller to present the second orange view controller and then the orange view controller should present the third white view controller.
这可以通过让第一个黑色视图控制器呈现第二个橙色视图控制器然后橙色视图控制器应该呈现第三个白色视图控制器来实现。
But this arises an issue which is: the end-user will clearly notice that there are two view controllers have been sequentially presented. For solving this, you will need to do a pretty simple trick which is to take a screenshot from the first view controller and passing it to the second view controller to display it while presenting the third view controller.
但是这产生了一个问题:最终用户将清楚地注意到已经顺序呈现了两个视图控制器。为了解决这个问题,你需要做一个非常简单的技巧,即从第一个视图控制器获取屏幕截图并将其传递给第二个视图控制器,以便在呈现第三个视图控制器时显示它。
You can check this repository to see how it is exactly could be done (Swift 3).
您可以检查此存储库以了解它是如何完成的(Swift 3)。
The final output would be:
最终的输出是:
#4
0
The most simple solution I can think is to hide the OrangeViewController and then show it when the WhiteViewController has been shown using the completion callback of present(_:animated:completion:) using this code on the button action.
我能想到的最简单的解决方案是隐藏OrangeViewController,然后在使用按钮操作上的代码使用present(_:animated:completion :)的完成回调显示WhiteViewController时显示它。
@IBAction func goToWhite(_ sender: UIButton) {
let orangeViewController = storyboard?.instantiateViewController(withIdentifier: "OrangeViewController")
orangeViewController?.view.isHidden = true
present(orangeViewController!, animated: false)
let whiteViewController = storyboard?.instantiateViewController(withIdentifier: "WhiteViewController")
orangeViewController?.present(whiteViewController!, animated: true) {
orangeViewController?.view.isHidden = false
}
}
#5
-2
you can append navigation controller to the first controller. Then on click of first view controller you can set all the view controllers in whatever order you want by using this code
您可以将导航控制器附加到第一个控制器。然后,在单击第一个视图控制器时,您可以使用此代码以您想要的任何顺序设置所有视图控制器
var viewControllers = self.navigationController.viewControllers.mutable
/ * Now you can append the other 2 controllers to this array */
viewControllers.append(yellowVC);
viewControllers.append(whiteVC);
self.navigationController.setViewCOntrollers(viewCOntrollers.animated:YES);
You can also change the order of popping the viewCOntrollers by using the above code.
您还可以使用上面的代码更改弹出viewCO控制器的顺序。
#1
2
Here is one method...
这是一种方法......
- Black presents Orange
- 黑色呈现橙色
- In
viewDidLoad
, Orange instantiates White as a childVC and adds White's view as a 'full-screen' subview. - 在viewDidLoad中,Orange将White实例化为childVC,并将White的视图添加为“全屏”子视图。
- On button tap in White, White's view is animated away and removed from the hierarchy.
- 在白色按钮上,白色视图被动画化并从层次结构中删除。
- On button tap in Orange, Orange is dismissed, returning user to Black.
- 在橙色按钮上,橙色被解除,使用户返回黑色。
Here is method two...
这是方法二......
- Embed Black in a NavigationController without navigation bar
- 在没有导航栏的NavigationController中嵌入Black
- On tap in Black, instantiate Orange and White VCs
- 点击黑色,实例化橙色和白色VC
- Call
.setViewControllers:animated:
to "stack the views" on the navigation controller, and jump directly to the last view. - 调用.setViewControllers:animated:在导航控制器上“堆叠视图”,并直接跳转到最后一个视图。
- After that, navigation backwards uses the standard
.popViewController
functionality. - 之后,向后导航使用标准的.popViewController功能。
You can view a working example of both methods here: https://github.com/DonMag/SkipNavigation
您可以在此处查看这两种方法的工作示例:https://github.com/DonMag/SkipNavigation
#2
1
If the black VC presents both the orange and immediately the white, you'll have the structure you're looking for. You can turn off animations so that the orange would probably not be visible.
如果黑色VC同时显示橙色和白色,您将拥有您正在寻找的结构。您可以关闭动画,以便橙色可能不可见。
#3
1
This could be achieved by letting the first black view controller to present the second orange view controller and then the orange view controller should present the third white view controller.
这可以通过让第一个黑色视图控制器呈现第二个橙色视图控制器然后橙色视图控制器应该呈现第三个白色视图控制器来实现。
But this arises an issue which is: the end-user will clearly notice that there are two view controllers have been sequentially presented. For solving this, you will need to do a pretty simple trick which is to take a screenshot from the first view controller and passing it to the second view controller to display it while presenting the third view controller.
但是这产生了一个问题:最终用户将清楚地注意到已经顺序呈现了两个视图控制器。为了解决这个问题,你需要做一个非常简单的技巧,即从第一个视图控制器获取屏幕截图并将其传递给第二个视图控制器,以便在呈现第三个视图控制器时显示它。
You can check this repository to see how it is exactly could be done (Swift 3).
您可以检查此存储库以了解它是如何完成的(Swift 3)。
The final output would be:
最终的输出是:
#4
0
The most simple solution I can think is to hide the OrangeViewController and then show it when the WhiteViewController has been shown using the completion callback of present(_:animated:completion:) using this code on the button action.
我能想到的最简单的解决方案是隐藏OrangeViewController,然后在使用按钮操作上的代码使用present(_:animated:completion :)的完成回调显示WhiteViewController时显示它。
@IBAction func goToWhite(_ sender: UIButton) {
let orangeViewController = storyboard?.instantiateViewController(withIdentifier: "OrangeViewController")
orangeViewController?.view.isHidden = true
present(orangeViewController!, animated: false)
let whiteViewController = storyboard?.instantiateViewController(withIdentifier: "WhiteViewController")
orangeViewController?.present(whiteViewController!, animated: true) {
orangeViewController?.view.isHidden = false
}
}
#5
-2
you can append navigation controller to the first controller. Then on click of first view controller you can set all the view controllers in whatever order you want by using this code
您可以将导航控制器附加到第一个控制器。然后,在单击第一个视图控制器时,您可以使用此代码以您想要的任何顺序设置所有视图控制器
var viewControllers = self.navigationController.viewControllers.mutable
/ * Now you can append the other 2 controllers to this array */
viewControllers.append(yellowVC);
viewControllers.append(whiteVC);
self.navigationController.setViewCOntrollers(viewCOntrollers.animated:YES);
You can also change the order of popping the viewCOntrollers by using the above code.
您还可以使用上面的代码更改弹出viewCO控制器的顺序。