我有3个VC(VC在ViewController中)
1 AuthViewController,它是应用程序的入口点,它检查用户是否已登录,否则会显示与登录相关的视图
2 HomeViewController是用户登录后应用程序登录页面的主界面
3 ListViewController,它显示一个列表,通过HomeViewController上的segue调用。
我想通过导航控制器启用ListViewController和HomeViewController之间的反向导航。
如果我有一个根VC为HomeVC的Navigation控制器,该如何实现。如何从AuthVC调用它,以便使用Navigation控制器调用它。
我试图调用导航控制器,但是没有用
我也尝试通过以下方式调用HomeVC
let mainStoryboard = UIStoryboard(name: "Main", bundle: Bundle.main)
if let viewController = mainStoryboard.instantiateViewController(withIdentifier: "mainViewController") as? UIViewController {
viewController.modalPresentationStyle = .overCurrentContext
self.present(viewController, animated: true, completion: nil)
虽然我能够调用HomeVC无法获得导航按钮
如何在不丢失NavigationController的情况下从AuthVC调用HomeVC
push
方法,而不是present
。进入HomeController后,您可以使用以下代码推送其他控制器,它将自动向您的控制器添加后退按钮。
let mainStoryboard = UIStoryboard(name: "Main", bundle: Bundle.main)
if let viewController = mainStoryboard.instantiateViewController(withIdentifier: "mainViewController") as? UIViewController {
self.navigationController?.pushViewController(vc, animated: true)
}
您还可以使用以下扩展名:
extension UIViewController {
func pushVC(storyboardName : String, vcname : String) {
let vc = UIStoryboard.init(name: storyboardName, bundle: Bundle.main).instantiateViewController(withIdentifier: vcname)
vc.hidesBottomBarWhenPushed = true
self.navigationController?.pushViewController(vc, animated: true)
}
func popVC() {
self.navigationController?.popViewController(animated: true)
}
func makeRootVC(storyBoardName : String, vcName : String) {
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let vc = UIStoryboard(name: storyBoardName, bundle: Bundle.main).instantiateViewController(withIdentifier: vcName)
let nav = UINavigationController(rootViewController: vc)
nav.navigationBar.isHidden = true
appDelegate.window?.rootViewController = nav // If using XCode 11 and above, copy var window : UIWindow? in your appDelegate file
let options: UIView.AnimationOptions = .transitionCrossDissolve
let duration: TimeInterval = 0.6
UIView.transition(with: appDelegate.window!, duration: duration, options: options, animations: {}, completion: nil)
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句