【原】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() )

你可能感兴趣的:(【原】swift基础篇之页面跳转)