軟體開發隨筆

Swift (2): about the Navigation

FB公開社團 Larry的午茶時光
加入不需審核,歡迎讀者加入~
我的IG帳號: larry.time.taste。剛剛起步,歡迎大家追蹤~

當你將一個 Navigation Controller 加入到 View (或是 View Controller, 效果一樣),storyboard 上會新增一個 Navigation Controller Scene, 該 View 的同一層 (以 Document Outline 的架構來說) 會有一個 Navigation Item (也可以稱之為 navigation bar). 新增的 Navigation Controller 會與 View Controller 以 pic1 連結,官方文件稱之 “The (table) view controller is the navigation controller's root view controller.”

pic1

拖拉一個 “Bar Button Item” 到 navigation bar, 在右邊 attributes inspector 可調整 “Bar Button Item” 為 Add, Edit Done, Cancel 等一般我們在 App 上方看到的。Control-drag 剛剛新增的 bar button 到你要到的 view controller, 兩個 view controller 會用 pic2 (或是 pic3) 做連結,切換過去的 view controller 左上角會自然出現 Back.


pic2

pic3

pic1, 2, 3 在蘋果官方文件稱之為 “segue” (發音類似 Segway, 順道一提,Segway 被中國的 Ninebot 買了下來,雷軍是 Ninebot 的股東,所以 10/19 在小米電視3的發佈會上雷軍也發佈了 Ninebot 的產品,這樣應該有助於記憶 segue 🙂

Segue 有幾種類型:push, modal, 等 (Xcode7是叫 show, present modally), 目前的理解是一般在 App 中切換頁面是用 push, modal 比較像之前 Win32 modal dialog 的概念,像是 add new item 就可以用 modal.
上面提到 push 切換過去的 view controller 左上角自然出現 back, 但是 modal 切換過去的 view controller 是完全空白的 (所以要 dismiss 自己)

About the Unwind Segue.

// In your origin view controller
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {


// In your destination view controller
@IBAction func unwindToYourDestinationView(sender: UIStoryboardSegue) {
}

將 storyboard origin view 的 Save button 拉到 origin view 上方的 Exit item, 選擇 unwindToYourDestinationView. 這樣按 Save 時就會觸發上方兩個 function (將 modal view 設定的資料傳回)。Cancel 的部分簡單藉由 Action handler, 在其中調用 dismissViewControllerAnimated 即可。

FB公開社團 Larry的午茶時光
加入不需審核,歡迎讀者加入~
我的IG帳號: larry.time.taste。剛剛起步,歡迎大家追蹤~

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *