iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 8
0
Mobile Development

《菜鳥のSwift》持續30天開發挑戰系列 第 8

《DAY 8》切換手機畫面

把畫面做出來之後,肯定要能夠切換畫面,今天就來介紹一下切換手機畫面的方式。

  • 放一個 Button 元件到 View Controller 上,再 control + 左鍵按住 Button 拉藍線到另一個 View Controller 上放開再選擇 Show 來產生 segue。如果有其它元件也會跳至同一頁,建議用下一個方法,因為只需要產生一條跳頁的 segue。
    https://ithelp.ithome.com.tw/upload/images/20200921/20129680AruNEgUTmz.png

  • 昨天的文章中已經有先使用過了,這麼做的好處是,只需要產生一個 segue,按鈕、表格裡的儲存格等等都可以利用 performSegue 函數進行跳頁。
    https://ithelp.ithome.com.tw/upload/images/20200921/201296801OuGBsIMLP.png

performSegue(withIdentifier: "toSecondView", sender: nil)
  • 接著是不使用 segue 跳頁的方式,當兩個手機畫面在不同的 storyboard 下時要進行跳頁,就得使用這個方法,當然手機畫面在相同的 storyboard 下也是可以使用的。

以不同的 storyboard 為例,先建一個新的 storyboard 並取好檔名。
https://ithelp.ithome.com.tw/upload/images/20200921/20129680iPz2jRk31h.png

新增 View Controller 到新建好的 storyboard 裡並取個 Storyboard ID(通常方便起見會跟 Class 一樣)。
https://ithelp.ithome.com.tw/upload/images/20200921/20129680eBrKnPXf5j.png

回到 ViewController.swift 在適當位置加入程式碼。

let myS = UIStoryboard(name: "Second", bundle: nil) // Second 是 storyboard 的檔名
let vc = myS.instantiateViewController(identifier: "SecondViewController") // SecondViewController 是 Storyboard ID
show(vc, sender: nil) // 使用 show 函數切換畫面

上一篇
《DAY 7》新增手機頁面
下一篇
《DAY 9》在畫面之間傳遞參數(上)
系列文
《菜鳥のSwift》持續30天開發挑戰30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言