4.UIViewController

UIViewController

UIViewController簡單的說就是視圖控制器, 用於view的管理, 數據的處理.

1. UIViewController的跳轉(無導航欄)

利用前面學習的知識, 我們實現下面的需求:

  • ViewController中有一個UIButton, 點擊UIButton跳轉到ViewController2
  • ViewController2中也有一個UIButton, 點擊UIButton返回到ViewController

ViewController的代碼:

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        let button = UIButton(type: .Custom)
        button.frame = CGRect(x: 0, y: 10, width: 100, height: 40)
        // 設置不同狀態文字
        button.setTitle("Normal", forState: .Normal)
        button.setTitle("Highlighted", forState: .Highlighted)
        button.setTitle("Disabled", forState: .Disabled)
        // 設置不同狀態文字顏色
        button.setTitleColor(UIColor.redColor(), forState: .Normal)
        button.setTitleColor(UIColor.blueColor(), forState: .Highlighted)
        button.setTitleColor(UIColor.grayColor(), forState: .Disabled)

        button.setBackgroundImage(UIImage(named: "normal"), forState: .Normal)
        button.setBackgroundImage(UIImage(named: "highlighted"), forState: .Highlighted)
        button.setBackgroundImage(UIImage(named: "disabled"), forState: .Disabled)

        button.addTarget(self, action: "buttonAction:", forControlEvents: .TouchUpInside)

        self.view.addSubview(button)
    }

    func buttonAction(sender: UIButton) {
        // 跳轉到ViewController2
        let vc = ViewController2()
        self.presentViewController(vc, animated: true, completion: nil)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()

    }


}

ViewController2代碼:

import UIKit


class ViewController2: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        self.view.backgroundColor = UIColor(red: 0xff/255.0, green: 0x00/255.0, blue: 0xff/255.0, alpha: 1.0)

        let button = UIButton(type: .Custom)
        button.frame = CGRect(x: 0, y: 200, width: 100, height: 40)
        // 設置不同狀態文字
        button.setTitle("返回", forState: .Normal)
        button.setTitle("返回", forState: .Highlighted)
        button.setTitle("返回", forState: .Disabled)
        // 設置不同狀態文字顏色
        button.setTitleColor(UIColor.redColor(), forState: .Normal)
        button.setTitleColor(UIColor.blueColor(), forState: .Highlighted)
        button.setTitleColor(UIColor.grayColor(), forState: .Disabled)

        button.setBackgroundImage(UIImage(named: "normal"), forState: .Normal)
        button.setBackgroundImage(UIImage(named: "highlighted"), forState: .Highlighted)
        button.setBackgroundImage(UIImage(named: "disabled"), forState: .Disabled)

        button.addTarget(self, action: "backAction:", forControlEvents: .TouchUpInside)

        self.view.addSubview(button)
    }

    func backAction(sender: UIButton) {
        // 返回
        self.dismissViewControllerAnimated(true, completion: nil)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

}

我們看看實現的效果:

界面跳轉

我們看看界面跳轉的方法

public func presentViewController(viewControllerToPresent: UIViewController, animated flag: Bool, completion: (() -> Void)?)
第一個參數是要跳轉的UIViewController
第二個參數是是否有動畫
第三個參數是完成回調

界面返回方法

public func dismissViewControllerAnimated(flag: Bool, completion: (() -> Void)?)
第一個參數是否有動畫
第二個參數是完成回掉

2. 界面跳轉動畫

self.modalTransitionStyle = .FlipHorizontal // 水平切換
self.modalTransitionStyle = .CoverVertical // 垂直切換
self.modalTransitionStyle = .CrossDissolve 
self.modalTransitionStyle = .PartialCurl // 翻頁

可以都試一下看看效果

3.UIViewController狀態轉換

狀態轉換

  • public func viewWillAppear(animated: Bool) => UIViewController將出現調用
  • public func viewDidAppear(animated: Bool) =>UIViewController已出現調用
  • public func viewWillDisappear(animated: Bool) => UIViewController將消失調用
  • public func viewDidDisappear(animated: Bool) => UIViewController已消失調用

我們可以注意到,還有一個方法:
public func viewDidLoad() => 在第一次加載view時調用,只調用一次, 而上面的4個方法每次都會調用

我們重寫上面的方法看看執行順序, 修改ViewController2:

class ViewController2: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        print("viewDidLoad")

        self.view.backgroundColor = UIColor(red: 0xff/255.0, green: 0x00/255.0, blue: 0xff/255.0, alpha: 1.0)

        let button = UIButton(type: .Custom)
        button.frame = CGRect(x: 0, y: 200, width: 100, height: 40)
        // 設置不同狀態文字
        button.setTitle("返回", forState: .Normal)
        button.setTitle("返回", forState: .Highlighted)
        button.setTitle("返回", forState: .Disabled)
        // 設置不同狀態文字顏色
        button.setTitleColor(UIColor.redColor(), forState: .Normal)
        button.setTitleColor(UIColor.blueColor(), forState: .Highlighted)
        button.setTitleColor(UIColor.grayColor(), forState: .Disabled)

        button.setBackgroundImage(UIImage(named: "normal"), forState: .Normal)
        button.setBackgroundImage(UIImage(named: "highlighted"), forState: .Highlighted)
        button.setBackgroundImage(UIImage(named: "disabled"), forState: .Disabled)

        button.addTarget(self, action: "backAction:", forControlEvents: .TouchUpInside)

        self.view.addSubview(button)

        self.modalTransitionStyle = .CrossDissolve
    }

    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        print("viewWillAppear")
    }

    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        print("viewDidAppear")
    }

    override func viewWillDisappear(animated: Bool) {
        super.viewWillDisappear(animated)
        print("viewDidAppear")
    }

    override func viewDidDisappear(animated: Bool) {
        super.viewDidDisappear(animated)
        print("viewDidDisappear")
    }

    func backAction(sender: UIButton) {
        // 返回
        self.dismissViewControllerAnimated(true, completion: nil)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

}

運行程序,我們發現:

  • 進入ViewController2的時候執行的方法依次是:
    viewDidLoad => viewWillAppear => viewDidAppear
  • 離開ViewController2的時候執行的方法依次是:
    viewWillDisappear => viewDidDisappear

4. 界面間跳轉傳值

現在我們實現一個需求, ViewController跳轉到ViewController2時,傳一個年齡過去:

我們在ViewController2中添加一個age變量:

var age: Int?

然後在ViewController的跳轉方法中賦值:

func buttonAction(sender: UIButton) {
        // 跳轉到ViewController2
        let vc = ViewController2()
        vc.age = 100
        self.presentViewController(vc, animated: true, completion: nil)
}

最後在ViewController的viewDidLoad()中添加:

print("age=\(self.age!)")

運行程序我們發現,控制檯中打印出:
界面間跳轉傳值

5. 加載storyboard中的UIViewController

如果使用storyboard做界面, 雖然可以直接在兩個UIViewController中拖線跳轉, 但是有時候一個UIViewController根據條件跳轉到不同的UIViewController, 這時候就需要用代碼加載storyboard中的UIViewController,再根據條件來跳轉.

在Main.storyboard中拖一個UIViewController,Storyboard ID設置爲ViewControllerInSB
storyboard

let mainStoryboard = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle())
let vc = mainStoryboard.instantiateViewControllerWithIdentifier("ViewControllerInSB")
self.presentViewController(vc, animated: true, completion: nil)

6. 完整代碼

ViewController.swift:

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        let button = UIButton(type: .Custom)
        button.frame = CGRect(x: 0, y: 10, width: 100, height: 40)
        // 設置不同狀態文字
        button.setTitle("Normal", forState: .Normal)
        button.setTitle("Highlighted", forState: .Highlighted)
        button.setTitle("Disabled", forState: .Disabled)
        // 設置不同狀態文字顏色
        button.setTitleColor(UIColor.redColor(), forState: .Normal)
        button.setTitleColor(UIColor.blueColor(), forState: .Highlighted)
        button.setTitleColor(UIColor.grayColor(), forState: .Disabled)

        button.setBackgroundImage(UIImage(named: "normal"), forState: .Normal)
        button.setBackgroundImage(UIImage(named: "highlighted"), forState: .Highlighted)
        button.setBackgroundImage(UIImage(named: "disabled"), forState: .Disabled)

        button.addTarget(self, action: "buttonAction:", forControlEvents: .TouchUpInside)

        self.modalTransitionStyle = .CrossDissolve
        self.view.addSubview(button)
    }

    func buttonAction(sender: UIButton) {
        // 跳轉到ViewController2
        let vc = ViewController2()
        vc.age = 100
        self.presentViewController(vc, animated: true, completion: nil)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()

    }
}

ViewController2.swift:

class ViewController2: UIViewController {

    var age: Int?

    override func viewDidLoad() {
        super.viewDidLoad()

        print("viewDidLoad")
        print("age=\(self.age!)")

        self.view.backgroundColor = UIColor(red: 0xff/255.0, green: 0x00/255.0, blue: 0xff/255.0, alpha: 1.0)

        let button = UIButton(type: .Custom)
        button.frame = CGRect(x: 0, y: 200, width: 100, height: 40)
        // 設置不同狀態文字
        button.setTitle("返回", forState: .Normal)
        button.setTitle("返回", forState: .Highlighted)
        button.setTitle("返回", forState: .Disabled)
        // 設置不同狀態文字顏色
        button.setTitleColor(UIColor.redColor(), forState: .Normal)
        button.setTitleColor(UIColor.blueColor(), forState: .Highlighted)
        button.setTitleColor(UIColor.grayColor(), forState: .Disabled)

        button.setBackgroundImage(UIImage(named: "normal"), forState: .Normal)
        button.setBackgroundImage(UIImage(named: "highlighted"), forState: .Highlighted)
        button.setBackgroundImage(UIImage(named: "disabled"), forState: .Disabled)

        button.addTarget(self, action: "backAction:", forControlEvents: .TouchUpInside)

        self.view.addSubview(button)

        self.modalTransitionStyle = .CrossDissolve
    }

    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        print("viewWillAppear")
    }

    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        print("viewDidAppear")
    }

    override func viewWillDisappear(animated: Bool) {
        super.viewWillDisappear(animated)
        print("viewWillDisappear")
    }

    override func viewDidDisappear(animated: Bool) {
        super.viewDidDisappear(animated)
        print("viewDidDisappear")
    }

    func backAction(sender: UIButton) {
        // 返回
        self.dismissViewControllerAnimated(true, completion: nil)
    }


    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章