今天我們練習用輸入框,來換頁吧~
首先建立第三個畫面的檔案,建立好後
然後我們到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是預設的藍色
跳轉到你要的畫面,就會出現跳轉成功的提示框了~~
成果: