【原】swift基础篇之页面跳转

常用的跳转方法有四种:present、dissmiss、push、pop.前两中不含导航栏跳转、后两种导航栏

1.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)

2.dismiss:返回

  self.dismiss(animated: true, completion: nil)

3.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)

4.pop返回

 self.navigationController?.popViewController(animated: true)
【拓展-如何代码中给页面添加导航栏】

1.给第一个根控制器添加导航栏

  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() )

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容