zoukankan      html  css  js  c++  java
  • [Xcode 实际操作]三、视图控制器-(5)使用UINavigationController视图入栈和出栈

    目录:[Swift]Xcode实际操作

    本文将演示使用导航控制器的几个跳转方式

    选择编辑第二个视图控制器文件

      1 import UIKit
      2 //定义一个全局变量,用来记录当前显示区域的视图的编号
      3 var pageNum = 0
      4 
      5 class SecondSubViewController: UIViewController {
      6 
      7     override func viewDidLoad() {
      8         super.viewDidLoad()
      9 
     10         // Do any additional setup after loading the view.
     11         //每当当前视图控制器被创建依此,全局变量即增加1
     12         pageNum = pageNum + 1
     13         //然后根据当前的全局变量数值,设置视图控制器的标题名称
     14         self.title = "Page(pageNum)"
     15         //设置视图控制器的背景颜色为紫色
     16         self.view.backgroundColor = UIColor.purple
     17         
     18         //添加一个按钮,当点击按钮时,跳转(Push)至下一页面
     19         let push = UIButton(frame: CGRect(x: 40, y: 120,  240, height: 40))
     20         //设置按钮上的标题文字
     21         push.setTitle("Push Page", for: UIControl.State())
     22         //设置按钮的背景颜色为橙色
     23         push.backgroundColor = UIColor.orange
     24         //给按钮绑定点击事件
     25         push.addTarget(self, action: #selector(SecondSubViewController.pushPage), for: UIControl.Event.touchUpInside)
     26         //将按钮添加到当前视图控制器的根视图
     27         self.view.addSubview(push)
     28         
     29         //再添加一个按钮,当点击按钮时
     30         //从当前页面返回(Pop)至上一级页面
     31         let pop = UIButton(frame: CGRect(x: 40, y: 180,  240, height: 40))
     32         //设置按钮上的标题文字
     33         pop.setTitle("Pop Page", for: UIControl.State())
     34         //设置按钮的背景颜色为橙色
     35         pop.backgroundColor = UIColor.orange
     36         //给按钮绑定点击事件
     37         pop.addTarget(self, action: #selector(SecondSubViewController.popPage), for: UIControl.Event.touchUpInside)
     38         //将按钮添加到当前视图控制器的根视图
     39         self.view.addSubview(pop)
     40         
     41         //再添加一个按钮,当点击按钮时
     42         //跳转(Pop)到指定序号的视图
     43         let index = UIButton(frame: CGRect(x: 40, y: 280,  240, height: 40))
     44         //设置按钮上的标题文字
     45         index.setTitle("Goto Index Page", for: UIControl.State())
     46         //设置按钮的背景颜色为橙色
     47         index.backgroundColor = UIColor.orange
     48         //给按钮绑定点击事件
     49         index.addTarget(self, action: #selector(SecondSubViewController.gotoIndexPage), for: UIControl.Event.touchUpInside)
     50         //将按钮添加到当前视图控制器的根视图
     51         self.view.addSubview(index)
     52         
     53         //继续添加一个按钮,当点击按钮时
     54         //跳转(Pop)到根视图
     55         let root = UIButton(frame: CGRect(x: 40, y: 340,  240, height: 40))
     56         //设置按钮上的标题文字
     57         root.setTitle("Goto Root Page", for: UIControl.State())
     58         //设置按钮的背景颜色为橙色
     59         root.backgroundColor = UIColor.orange
     60         //给按钮绑定点击事件
     61         root.addTarget(self, action: #selector(SecondSubViewController.gotoRootPage), for: UIControl.Event.touchUpInside)
     62         //将按钮添加到当前视图控制器的根视图
     63         self.view.addSubview(root)
     64     }
     65     
     66     //创建第一个按钮绑定的方法
     67     @objc func pushPage()
     68     {
     69         //实例化第二个视图控制器
     70         let viewController = SecondSubViewController()
     71         //把视图控制器,Push到导航视图里,相当于入栈操作
     72         self.navigationController?.pushViewController(viewController, animated: true)
     73     }
     74     
     75     //创建第二个按钮绑定的方法
     76     @objc func popPage()
     77     {
     78         //当前视图控制器,将从导航视图控制器堆栈中移除,
     79         //并返回至上一页面,相当于出栈操作。
     80         self.navigationController?.popViewController(animated: true)
     81     }
     82     
     83     //创建第三个按钮绑定的方法
     84     @objc func gotoIndexPage()
     85     {
     86         //根据导航视图控制器中的全局序号,
     87         //查找堆栈中指定序号的视图控制器
     88         let viewController = self.navigationController?.viewControllers[2]
     89         //然后在导航视图控制器中,跳转至该视图控制器
     90         self.navigationController?.popToViewController(viewController!, animated: true)
     91     }
     92     
     93     //创建第四个按钮绑定的方法
     94     @objc func gotoRootPage()
     95     {
     96         //当用户点击第四个按钮时,
     97         //导航控制器中的所有子视图控制器,都将全部出栈,
     98         //从而跳转至根视图控制器
     99         self.navigationController?.popToRootViewController(animated: true)
    100     }
    101 
    102     override func didReceiveMemoryWarning() {
    103         super.didReceiveMemoryWarning()
    104         // Dispose of any resources that can be recreated.
    105     }
    106 }
  • 相关阅读:
    JS_Boolean Logic
    js String
    .Net之路(二)简介
    自考 操作系统概论计算机系统
    IT大学生最重要的五个能力
    数据库表及字段命名规范
    简述MVC分层
    .Net之路(一)概述
    设计模式(4)迭代器模式
    .Net之路(三)如何连接数据库?
  • 原文地址:https://www.cnblogs.com/strengthen/p/9986994.html
Copyright © 2011-2022 走看看