zoukankan      html  css  js  c++  java
  • iOS第八课——Navigation Controller和Tab bar Controller

    今天我们要学习Navigation Controller和Tab bar Controller。

    Navigation Controller是iOS编程中比较常用的一种容器,用来管理多个视图控制器。
    UINavigationController从上往下看,由Navigation bar ,Navigation View ,Navigation toobar等组成。 
    页面跳转(两种类型):NavigationViewController跳转(向右前进,向左返回)、ViewContorller跳转(自上而下)。
    第一种跳转:通过StoryBoard设置界面的步骤:通过前页的button创建segue(有了segue,push方法才能生效),在前页的viewController中创建button对应的action方法,在方法里面——>先创建一个Storyboard的对象,然后设置viewController的storyboard identifier,通过Storyboard的对象创建刚才id号对应的viewController对象,就可以通过pushViewController跳转到指定的ViewController
    let myStoryboard = UIStoryboard(name: "Main", bundle: nil)
    let vc2 = myStoryboard.instantiateViewController(withIdentifier: "Sec") as! SecViewController
    self.navigationController?.pushViewController(vc2, animated: true)
            
    let vc3 = myStoryboard.instantiateViewController(withIdentifier: "Third") as! ThirdViewController
    self.navigationController?.pushViewController(vc3, animated: true)

    然后通过popViewController返回到指定的viewController(从右侧进入):

    @IBAction func btn(_ sender: UIButton) {
            //前提:由pushView方法跳转而来,三种方法同时只能用一种
            //方式1:跳转到前一个页面
    //        _ = self.navigationController?.popViewController(animated: true)
            //方式二:返回到第一个ViewContoller
    //        _ = self.navigationController?.popToRootViewController(animated: true)
            //方式三:跳转到指定页面
            for i in 0..<(self.navigationController?.viewControllers.count)! {
                if self.navigationController?.viewControllers[i].isKind(of: SecViewController.self) == true {
                    _ = self.navigationController?.popToViewController(self.navigationController?.viewControllers[i] as! SecViewController, animated: true)
                    break
                }
            }
    

     第二种——自下而上:向上弹出,向下返回(通过弹出模式框的方式自下而上的打出viewController):

    @IBAction func btn(_ sender: UIButton) {
            let myStoryboard = UIStoryboard(name: "Main", bundle: nil)
            let vc2 = myStoryboard.instantiateViewController(withIdentifier: "Sec") as! SecViewController
            self.present(vc2, animated: true, completion: nil)
        }
    

     这一个就是真的“返回”了:

    @IBAction func backward(_ sender: UIButton) {
            self.dismiss(animated: true, completion: nil)
        }
    

     通过segue向下页跳转(iOS8之后):Action Segue:Show、Show Detail、Present Modally、Present As Popover、Custom。(具体含义,遇再论)。

    在顶部Navigation Bar的Navigation Item(下级页面返回键)——是谁的返回键,就返回到谁:

    修改其返回键的显示:

    1、默认显示该Item的Title;2、也可以用Back Button更为灵活的显示(设置这个就显示这个,不再通过Title显示,就可以不与其冲突等等)

    返回页的Navigation Item如果没有,就应该在该页的顶部Navigation Bar添加一个。

    在底部Navigation toolbar的Bar Button Item:

    在控件库中,将Bar Button Item拖到底部,可以自定义,也可以选择系统提供的图标库。

    Navigation Controller的传值方式:

    1、局部变量

    2、全局变量

    3、MVC模式(model以NSObject为父类)

    然后通过segue进行ViewController之间的传值,所以一定要设置segue的Identifier:

    前页向后页传值(在前页的prepare方法中进行传值):

    局部变量方式:

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
            // Get the new view controller using segue.destinationViewController.
            // Pass the selected object to the new view controller.
            if segue.identifier == "SecView" {
                let SecView = segue.destination as! SecViewController
                SecView.no = self.txt.text
            }
        }

    在后页(接受页):可以在viewDidLoad方法中接受——>self.txt.text = no。

    全局变量方式(在appDelgate中,设置全局变量var globle: String?):

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
            // Get the new view controller using segue.destinationViewController.
            // Pass the selected object to the new view controller.
            if segue.identifier == "SecView" {
                let appDelegate = UIApplication.shared.delegate as! AppDelegate
                appDelegate.globle = self.txt.text
            }
        }
    

     MVC模式(前页后页都要声明model的变量,而且一定要在适当的时候实例化,所以model类一定要写init方法):

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
            // Get the new view controller using segue.destinationViewController.
            // Pass the selected object to the new view controller.
            if segue.identifier == "SecView" {
                stu = StuEnity(self.txt.text!)
                let SecView = segue.destination as! SecViewController
                SecView.stu = stu!
            }
        }

    还有一种比较简单、也比较常用的方法——使用单例:

    首先创建单例:

    final class Single: NSObject {
        //创建单例有两种方法
        //使用全局变量创建单例
        static let shared = Single()
        public var name = ""
        private override init() {
            super.init()
        }
        
        //类方法,使用结构体创建单例
        class func shareInstance() -> Single {
            struct single{
                static var g_Instance = Single()
            }
            return single.g_Instance
        }
    }
    

     然后前页可以向后页传值,后页也可以向前页传值:

    //前页的代码:
    class ViewController: UIViewController {
        
        let data = Single.shared
    
        @IBOutlet weak var nameLabel: UITextField!
        
        override func viewDidLoad() {
            super.viewDidLoad()
            self.nameLabel.text = data.name
        }
        
        override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
            data.name = self.nameLabel.text!
        }
        
    }
    //后页的代码:
    class SecViewController: UIViewController {
        
        let data = Single.shared
    
        @IBOutlet weak var nameTextField: UITextField!
        
        override func viewDidLoad() {
            super.viewDidLoad()
            self.nameTextField.text = data.name
        }
        
        @IBAction func back(_ sender: UIButton) {
            data.name = self.nameTextField.text!
            let stb = UIStoryboard(name: "Main", bundle: nil)
            let back = stb.instantiateViewController(withIdentifier: "FirstView")
            self.present(back, animated: true, completion: nil)
        }
        
    }

    先放一个简易版本:http://files.cnblogs.com/files/quanxi/test.zip。

    然后还是将使用单例的简单例子放上来:http://download.csdn.net/download/leaf_and_wind/9726433

  • 相关阅读:
    【我所认知的BIOS】—&gt; uEFI AHCI Driver(4) — 第一个Protocol真难搞
    IOS Core Animation Advanced Techniques的学习笔记(四)
    Redhat 7改动默认执行级别方法 --RHEL7使用systemd创建符号链接指向默认执行级别
    CF1088F Ehab and a weird weight formula
    2018-2-13-win10-uwp-csdn-博客阅读器
    2018-2-13-win10-uwp-csdn-博客阅读器
    2018-2-13-win10-UWP-九幽数据分析
    2018-2-13-win10-UWP-九幽数据分析
    2019-9-2-win10-uwp-获得焦点改变
    2019-9-2-win10-uwp-获得焦点改变
  • 原文地址:https://www.cnblogs.com/quanxi/p/6139800.html
Copyright © 2011-2022 走看看