2014-10-10 00:00

[Swift-d24] - 實戰開發 - TODOList - Create View

Github link

今天要來建立 “新增資料” 的頁面

不過這個頁面我們也會拿來給 “更新資料” 的頁面使用

原因其實很簡單

兩邊要建立或編輯的資料是一樣的

差別只在於,當今天是編輯資料的 view call 該頁面時,

該頁面除了顯示編輯框外,編輯框內的文字也會預先 load 好舊的資料了

除此之外兩個 view 的排版一模一樣

所以在這邊我們就只建立一個 UpdateViewController 就可以了

那我們一樣建立一組 UpdateViewController.swift + UpdateViewController.xib

也一樣為元件建立關聯

image

回到主頁

我們幫 Add 新增一個按鈕吧

在 viewDidLoad 中

self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Add", style: UIBarButtonItemStyle.Done, target: self, action: Selector("pushToAddTODO"))

設定 Navigation 的右邊鍵的功能

並且根據 pushToAddTODO 的方法做事情

因此繼續下去將 pushToAddTODO 方法做一下

	func pushToAddTODO(){
        var addViewController = UpdateViewController(nibName: "UpdateViewController", bundle: nil)
        addViewController.from = "add"
        
        self.navigationController?.pushViewController(addViewController, animated: true)
    }

這樣寫完後就可以執行看看了!

剩下的任務就是將資料寫進 fakeData 不過考慮到之後我們是使用 API

因此就先不特別作 ShowViewController 的資料傳遞

再來就做 create view 裡面的 save 按鈕!

接著就要到 UpdateViewController

幫 NavigationController 再多加一個右邊按鈕 “Save”

import UIKit

class UpdateViewController: UIViewController {

    var from: String!
    @IBOutlet var textField: UITextField!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Save", style: UIBarButtonItemStyle.Done, target: self, action: Selector("save"))
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    
    func save() {
        self.navigationController?.popToRootViewControllerAnimated(true)
    }
}

整個程式碼都不難, 只有簡單處理 push/pop 的問題而已!

如果我們要加上對 ViewController 的 fakeData 做存取的話,請看以下:

由於需要資料傳遞,因此就必須寫 protocol

protocol 可簡單可複雜,今天會寫非常簡單版!

首先先新建一個 swift 檔案, 名為 TodoProtocol.swift

內容如下:

protocol UpdateTODOlistDelegate {
    func addData(id: String, content: String)
}

接著到 UpdateViewController 中新建一個變數 屬於 UpdateTODOlistDelegate protocol:

var delegate: UpdateTODOlistDelegate!

並且設定 save 後的行為,會呼叫 protocol 中的

addData(id: String, content: String)

將欲新增的 id, content 都丟給該方法

func save() {
		//	"id" 在這邊是直接給死的
        delegate.addData("67", content: self.textField.text!)
        self.navigationController?.popToRootViewControllerAnimated(true)
    }

接著只要在主頁的程式碼中時做 protocol, 設定接到參數後的動作

就可以在接到參數後,將資料寫入 fakeData 了!

回到主頁的程式碼,class 必須先繼承 protocol,再實作

設定繼承後, 實作 addData, 將接到的資料寫入 fakeData:

func addData(id: String, content: String) {
        self.fakeData.append(["id": id, "content": content])
        
        dispatch_async(dispatch_get_main_queue(), {
            // must be "tableView!" not "tableView?"
            self.tableView!.reloadData()
        })
    }

記得將 addViewController 的 delegate 設定成 self

func pushToAddTODO(){
        var addViewController = UpdateViewController(nibName: "UpdateViewController", bundle: nil)
        addViewController.from = "add"
        addViewController.delegate = self
        
        self.navigationController?.pushViewController(addViewController, animated: true)
    }

執行起來就可以增加資料到 fakeData 了!

Share

comments powered by Disqus