iT邦幫忙

2021 iThome 鐵人賽

DAY 24
0
Mobile Development

IOS菜逼八連續30天挑戰系列 第 24

Day 24 XIB跳轉頁面以及UIAlertController的練習(2/3)

今天我們練習用輸入框,來換頁吧~

首先建立第三個畫面的檔案,建立好後

然後我們到ThirdFrame.swift的ViewDidLoad把AlertController寫進去

代碼:

alertController = UIAlertController(title: "畫面跳轉成功", message: "現在是第3頁", preferredStyle: .alert)
    let alertAction = UIAlertAction(title: "ok", style: .default, handler: nil)
    alertController.addAction(alertAction)
    present(alertController, animated: true, completion: nil)

第二頁也是一樣做法~

接著到MainVC,把TextField的outlet拉好,之後輸入下面這段代碼

let AlertController = UIAlertController(title: "切換頁面?", message: "按下OK後跳轉", preferredStyle: .actionSheet)
    
    let AlertAction = UIAlertAction(title: "OK", style: .destructive, handler: { action in
            
        let page = self.changePageTextfiled.text
            
        switch page {
            
        case "2":
            let switchToSecondScreen = SecondFrame()
            self.navigationController?.pushViewController(switchToSecondScreen, animated: true)
                
        case "3" :
            let switchToThirdScreen = ThirdFrame()
            self.navigationController?.pushViewController(switchToThirdScreen, animated: true)
                
            default:
                break
            }
        })

這邊是用switch case ,讓你可以輸入字串然後跳到你要的頁面,然後我們把它寫進去alertController,這樣我們按下ok後才會跳轉喔~

preferredStyle: .actionSheet(是從下面彈出來~)
style: .destructive(是把ok設成紅色).default是預設的藍色

跳轉到你要的畫面,就會出現跳轉成功的提示框了~~

成果:


上一篇
Day 23 XIB跳轉頁面以及UIAlertController的練習(1/3)
下一篇
Day 25 XIB跳轉頁面以及UIAlertController的練習(3/3)
系列文
IOS菜逼八連續30天挑戰30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言