Day 25 XIB跳转页面以及UIAlertController的练习(3/3)

昨天的Textfield我们其实也可以改成这样:

 @IBAction func changePageAction(_ sender: Any) {
        
        
        let AlertController = UIAlertController(title: "切换页面?", message: "按下OK後跳转", preferredStyle: .alert)
        
        func option(optionTitle: String, optionStyle: UIAlertAction.Style ,mergehandler: ((UIAlertAction) -> Void)?) {
            AlertController.addAction(UIAlertAction(title: optionTitle, style: optionStyle, handler: mergehandler))
        }
        option(optionTitle: "2" , optionStyle: UIAlertAction.Style.default, mergehandler: { action in
            
            let switchToSecondScreen = SecondFrame()
            self.navigationController?.pushViewController(switchToSecondScreen, animated: true)
            
        })
        option(optionTitle: "3" , optionStyle: UIAlertAction.Style.default, mergehandler: { action in
            
            let switchToThirdScreen = ThirdFrame()
            self.navigationController?.pushViewController(switchToThirdScreen, animated: true)
            
        })
        option(optionTitle: "取消" , optionStyle: UIAlertAction.Style.destructive, mergehandler: { action in
            
        })
        
        present(AlertController, animated: true, completion: nil)

当然我们也可以用输入框的方式做:

@IBAction func changePageAction(_ sender: Any) {
    
        let CancelAlertController = UIAlertController(title: "转换页面失败", message: "请重试!", preferredStyle: .alert)
        
        let AlertController = UIAlertController(title: "切换页面?", message: "输入你要跳转的页数", preferredStyle: .alert)
        
        AlertController.addTextField { textField in
            
            textField.placeholder = "page?"
            
        }
        
        let alerOktAction = UIAlertAction(title: "ok", style: .default) {[unowned AlertController] _ in
            
            let page = AlertController.textFields?[0].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:
                self.present(CancelAlertController, animated: true, completion: nil)
            }
        }
        
        let TryagainAlertAction = UIAlertAction(title: "OK", style: .destructive, handler: nil)
        
        let alertCancelAction = UIAlertAction(title: "Cancel", style: .destructive, handler: nil)
        
        AlertController.addAction(alerOktAction)
        
        AlertController.addAction(alertCancelAction)
        
        CancelAlertController.addAction(TryagainAlertAction)
        
        present(AlertController, animated: true, completion: nil)
        
    }
    
}

成果:


<<:  团队文化

>>:  Angular Stock登入(二)(Day22)

Day 6 轻松了解欧洲个资隐私保护

在完整完善的隐私保护设计之下,也需有个健全的隐私保护法规在背後做支撑,藉由完善法规制度的保护之下,视...

Day 26 : Linux - 档案or目录的权限该怎麽看?又该如何做更改?

如标题,今天想和大家聊聊权限这东西 权限在Linux是个非常非常重要的东西,如果你一直被termin...

冒险村04 - Create PR with default template

04 - Create PR with default template 在 Github 多人开发...

网路是怎样连接的(七)TCP的交互(下)

思考重点 TCP如何确认对方收到消息? 讯息收发中的头部消息变化? 关闭连接操作? 核心知识 封包的...

[Day 3] 前後端技能这麽多,要选哪个呢?

本来打算一篇写完的,结果居然要分成三篇 XD 前端工具挑选 前端的部分可以搭配框架来建立 比较有名的...