swift基础篇之页面跳转
常用的跳转方法有四种:present、dissmiss、push、pop.前两中不含导航栏跳转、后两种导航栏
- present :跳转
let storyboard = UIStoryboard(name: "Main", bundle: nil)
guard let secondVC = storyboard.instantiateViewController(withIdentifier: "SecondViewControllerID") as? SecondViewController else {
return
}
secondVC.myStr="wrw"
self.present(secondVC, animated: true, completion: nil)
- dismiss:返回
self.dismiss(animated: true, completion: nil)
- push跳转(含导航栏)
let storyboard = UIStoryboard(name: "Main", bundle: nil)
guard let threeVC = storyboard.instantiateViewController(withIdentifier: "threeViewControllerID") as? threeViewController else {
return
}
self.navigationController?.pushViewController(threeVC, animated: true)
- pop返回
self.navigationController?.popViewController(animated: true)
【拓展-如何代码中给页面添加导航栏】
- 给第一个根控制器添加导航栏
let storyboard = UIStoryboard(name: "Main", bundle: nil)
guard let secondVC = storyboard.instantiateViewController(withIdentifier: "SecondViewControllerID") as? SecondViewController else {
return
}
let nav=UINavigationController(rootViewController: secondVC)
self.window?.rootViewController=nav
2.该页面没有导航栏,用present跳转,给下一个页面添加导航栏,且下一个页面可以push跳转:如给secondVC控制器添加导航栏。
let storyboard = UIStoryboard(name: "Main", bundle: nil)
guard let secondVC = storyboard.instantiateViewController(withIdentifier: "SecondViewControllerID") as? SecondViewController else {
return
}
secondVC.myStr="bbjjj"
let nav=UINavigationController(rootViewController: secondVC)
self.present(nav, animated: true, completion: nil)
【拓展-加载xib】
加载xib view:
let myView = Bundle.main.loadNibNamed("ActivityAlertView", owner: nil, options: nil)?.first as? ActivityAlertView
myView?.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height)
self.view.addSubview(myView ?? ActivityAlertView() )
本文由 创作,采用 知识共享署名4.0 国际许可协议进行许可。本站文章除注明转载/出处外,均为本站原创或翻译,转载前请务必署名。最后编辑时间为: 2021/06/01 09:28