简体   繁体   English

从UIView导航到没有Storyboard和导航控制器的UIViewController

[英]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,只要点按一次,我就想导航到另一个UIViewControllerBuyController() )。 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 . BarControllerUITabBarControllerUITabBarControllerDelegate )是我在AppDelegate.swift rootViewController。 The UIView however belongs to another UIViewController called HomeController ( UIViewController , UIScrollViewDelegate ). UIView但是属于另一种UIViewController称为HomeControllerUIViewControllerUIScrollViewDelegate )。 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.

相关问题 从UIView导航到UIViewController - Navigate to UIViewController from a UIView 如何从UIView导航UIViewController - How to Navigate UIViewController from UIView 如何从UIView子类导航到其他UIViewController - How to navigate from UIView subclass to other UIViewController 如何从UIView类导航到UIViewController? - How to Navigate from a UIView class to a UIViewController? 从Storyboard在UITableViewController上添加带标题的导航栏而没有导航控制器 - Add Navigation Bar with title without Navigation controller on UITableViewController from Storyboard 在 UIViewController Storyboard 中访问 UIView - access UIView In UIViewController Storyboard 将UIViewController视图属性设置为不带故事板/笔尖的自定义UIView类 - Set UIViewController view property to custom UIView class without storyboard / nib 如何通过按下图像导航到另一个UIView控制器(以前在情节提要中创建) - How to navigate to another UIView controller (previously created in storyboard) by pressing an image 如何使用情节提要segue将UIViewController连接到UIView,而无需任何代码? - How to connect UIViewController to UIView using storyboard segue, without any code? iOS-情节提要中的导航控制器 - iOS - navigation controller from storyboard
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM