[英]Navigate from UIView to UIViewController without storyboard and navigation controller
Is there a way to navigate from a UIView
, which is part of a UIViewController
, to another UIViewController
neither using storyboards nor navigation controllers? 有没有一种方法可以使用故事板或导航控制器从
UIViewController
一部分UIView
导航到另一个UIViewController
?
I have a UITapGestureRecognizer which I want to navigate to another UIViewController
( BuyController()
) whenever it is tapped once. 我有一个UITapGestureRecognizer,只要点按一次,我就想导航到另一个
UIViewController
( BuyController()
)。 Accordingly, my code looks like this: 因此,我的代码如下所示:
//handle singleTap
func singleTapOnLikesDetected(_ sender: UITapGestureRecognizer) {
print("check") //works
let toController = BuyController()
BarController().pushViewController(toController, animated: true) //error occurs since BarController is no navigationcontroller
}
BarController
( UITabBarController
, UITabBarControllerDelegate
) is my rootViewController in the AppDelegate.swift
. BarController
( UITabBarController
, UITabBarControllerDelegate
)是我在AppDelegate.swift
rootViewController。 The UIView
however belongs to another UIViewController
called HomeController
( UIViewController
, UIScrollViewDelegate
). 该
UIView
但是属于另一种UIViewController
称为HomeController
( UIViewController
, UIScrollViewDelegate
)。 I do not use storyboards at all and I would like to keep it that way, as I am going to work on a project in a team (thus, a programmatic solution would be very welcome)... 我根本不使用情节提要,而我想保持这种状态,因为我将在团队中进行项目工作(因此,非常欢迎使用编程解决方案)...
You can simply do this to navigate, without using navigationController
: 您只需执行此操作即可导航,而无需使用
navigationController
:
self.present(toController, animated: true, completion: nil)
Or Use this, 或使用这个
UIApplication.shared.keyWindow?.rootViewController?.present(toController, animated: true, completion: nil)
Only UIViewControllers can present other UIViewControllers not UIViews so your won't find the present method on the UIView. 只有UIViewControllers可以呈现其他UIViewControllers而不是UIViews,因此您将无法在UIView上找到present方法。 You also can't replace self with HomeController() because that creates a new HomeController with it's own UIView which hasn't been added to any other view (as the error suggests).
您也不能用HomeController()替换self,因为这会使用其自己的UIView创建一个新的HomeController,该UIView尚未添加到任何其他视图中(错误提示)。
You have a couple of options really: 您实际上有几个选择:
1) Create a protocol/delegate on your UIView and make the HomeController conform to it. 1)在您的UIView上创建一个协议/代理,并使HomeController符合它。 Then the UIView can call the method in the protocol and the HomeController do the actual switching.
然后,UIView可以调用协议中的方法,然后HomeController进行实际的切换。
2) You could get the root view controller to present the new UIViewController like this: UIApplication.shared.keyWindow?.rootViewController?.present(viewController, animated: true, completion: nil)
2)您可以让根视图控制器像下面这样呈现新的UIViewController:
UIApplication.shared.keyWindow?.rootViewController?.present(viewController, animated: true, completion: nil)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.