[英]Navigate from UIView to UIViewController without storyboard and navigation controller
有沒有一種方法可以使用故事板或導航控制器從UIViewController
一部分UIView
導航到另一個UIViewController
?
我有一個UITapGestureRecognizer,只要點按一次,我就想導航到另一個UIViewController
( BuyController()
)。 因此,我的代碼如下所示:
//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
)是我在AppDelegate.swift
rootViewController。 該UIView
但是屬於另一種UIViewController
稱為HomeController
( UIViewController
, UIScrollViewDelegate
)。 我根本不使用情節提要,而我想保持這種狀態,因為我將在團隊中進行項目工作(因此,非常歡迎使用編程解決方案)...
您只需執行此操作即可導航,而無需使用navigationController
:
self.present(toController, animated: true, completion: nil)
或使用這個
UIApplication.shared.keyWindow?.rootViewController?.present(toController, animated: true, completion: nil)
只有UIViewControllers可以呈現其他UIViewControllers而不是UIViews,因此您將無法在UIView上找到present方法。 您也不能用HomeController()替換self,因為這會使用其自己的UIView創建一個新的HomeController,該UIView尚未添加到任何其他視圖中(錯誤提示)。
您實際上有幾個選擇:
1)在您的UIView上創建一個協議/代理,並使HomeController符合它。 然后,UIView可以調用協議中的方法,然后HomeController進行實際的切換。
2)您可以讓根視圖控制器像下面這樣呈現新的UIViewController: UIApplication.shared.keyWindow?.rootViewController?.present(viewController, animated: true, completion: nil)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.