swift

Swift(十二)UIViewController

2017-08-23  本文已影响746人  YvanLiu

更新:2018.05.24

整理了一下demo:SwiftDemo


在iOS应用运行中,屏幕中显示的内容是一组一组的视图对象,他们负责显示屏幕中的内容,而在视图的后面是UIViewController视图控制器,它的作用是管理哪些视图中显示的内容,并协调他们和应用其他部分的关系。

UIViewController的主要作用:

视图控制器的生命周期:
  1. alloc
    创建一个视图控制器对象,并分配内存空间。
  2. init()
    对视图控制器对象进行初始化。
  3. loadView
    如果从storyboard创建视图,则从storyboard中加载视图。
  4. viewDidLoad
    视图加入完成,可以进行一些自定义操作
  5. viewWillAppear
    视图即将要展示在屏幕上。
  6. viewDidAppear
    视图已经站在屏幕上显示并完成渲染。
  7. viewWillLayoutSubviews
    视图即将布局其子视图
  8. viewDidLayoutSubviews
    视图已经完成子视图的布局
  9. viewWillDisappear
    视图即将从屏幕中消失
  10. viewDidDisappear
    视图已经从屏幕上消失
  11. dealloc
    视图被销毁
创建一个UIViewController

相信大家都知道MVC,在MVC中,UIViewController扮演者控制层(C)的角色,UIViewController的职责是:对内管理与之关联的UIView,对外跟其他UIViewController通信和协调。

创建UIViewController之前,我们先新建一个项目,新建项目后,可以使用代码活通过StoryBoard创建一个UIViewController。


一般情况下,我们选择Single View Application来创建一个新项目

填写项目名,Language选Swift,表示使用Swift语言。

可以看到新项目里已经有一个ViewController项目,而不同于OC的是,没有了.h.m文件,只有一个.swift后缀的文件。

一般在我们正常开发过程中,新建项目的这个ViewController很难满足我们的要求,比如我们的rootViewController需要的是一个有导航条的ViewController,那么我们就要自己创建一个。


点击New File
选择Cocoa Touch Class
输入一个类名,并选UIViewController

OK,我们创建了一个新的UIViewController:RootViewControlelr,现在我们来修改初始视图控制器:使用代码修改初始视图控制器在Swift中要比OC中简单的多,只需要在AppDelegat中的didFinishLaunchingWithOptions方法中设置window.rootViewController


因为上面我们说要加一个带有导航条的ViewController。所以这里设置的是UINavigatioController

修改新的视图控制器
我们来写一个例子,在RootViewController中添加一个按钮,点击按钮跳入另一个视图控制器中,然后在另一个视图控制器中添加一个按钮,点击退回到RootViewController中。

rootViewController中:

import UIKit

class RootViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.green
        
        let button = UIButton(type: .custom)
        button.frame = CGRect(x: 0, y: 200, width: UIScreen.main.bounds.width, height: 50)
        button.backgroundColor = UIColor.white
        button.setTitle("打开新的视图控制器", for: .normal)
        button.setTitleColor(UIColor.gray, for: .normal)
        view.addSubview(button)
        button.addTarget(self, action: #selector(buttonClick), for: .touchUpInside)
    }

    func buttonClick() {
        navigationController?.pushViewController(ViewController(), animated: true)
        // 另一种跳转方式
        // present(ViewController(), animated: true, completion: nil)
    }

}

ViewController中

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.red
       
        let button = UIButton(type: .custom)
        button.frame = CGRect(x: 0, y: 200, width: UIScreen.main.bounds.width, height: 50)
        button.backgroundColor = UIColor.white
        button.setTitle("回到RootViewController", for: .normal)
        button.setTitleColor(UIColor.gray, for: .normal)
        view.addSubview(button)
        button.addTarget(self, action: #selector(buttonClick), for: .touchUpInside)
    }

    func buttonClick() {
        navigationController?.popViewController(animated: true)
        // 另一种跳转方式 成对出现
        dismiss(animated: true, completion: nil)
    }
}

上一篇 下一篇

猜你喜欢

热点阅读