ios swift5 弹出原生的提示框(弹框) UIAlertController
生活随笔
收集整理的這篇文章主要介紹了
ios swift5 弹出原生的提示框(弹框) UIAlertController
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章目錄
- 1.從中間彈出,限制textfield的內容長度
- 1.1 截圖
- 1.2 代碼
- 1.3 參考
- 2.從下面彈出
- 2.1 截圖
- 2.2 代碼
- 3.其他舉例
- 3.1 下面只有一個確定按鈕(帶截圖和代碼)
- 3.2 只有代碼
- 4.參考博客
1.從中間彈出,限制textfield的內容長度
1.1 截圖
1.2 代碼
func renameAlert() {let cell: UITableViewCell = tableView.cellForRow(at: IndexPath(row: 0, section: 1))!let alertController = UIAlertController(title: NSLocalizedString("vehicleRename", comment: ""),message: nil, preferredStyle: .alert)let cancelAction = UIAlertAction(title: "取消", style: .cancel, handler: nil)let okAction = UIAlertAction(title: "確定", style: .default, handler: {action inprint("點擊了確定")let textField: UITextField = (alertController.textFields?[0])!;print(textField.text)cell.detailTextLabel?.text = textField.text//跳到定位設置})alertController.addTextField { (textfield) inprint("打印")//這個block會在彈出對話框的時候調用,這個參數textfield就是系統為我們創建的textfieldtextfield.delegate = selfprint(textfield)}alertController.addAction(cancelAction)alertController.addAction(okAction)self.present(alertController, animated: true, completion: nil)}extension settingVC: UITextFieldDelegate {func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {if range.location >= 6 {return false}return true}}1.3 參考
限制UITextField輸入長度的方法
2.從下面彈出
2.1 截圖
2.2 代碼
func bottomAlert() {let alertController = UIAlertController(title: NSLocalizedString("unitSwitch", comment: ""),message: nil, preferredStyle: .actionSheet)let cancelAction = UIAlertAction(title: NSLocalizedString("cancel", comment: ""), style: .cancel, handler: nil)let cell: UITableViewCell = tableView.cellForRow(at: IndexPath(row: 1, section: 1))!let kmAction = UIAlertAction(title: NSLocalizedString("km", comment: ""), style: .default, handler: {action inprint("點擊了公里")cell.detailTextLabel?.text = NSLocalizedString("km", comment: "")//跳到定位設置})let milesAction = UIAlertAction(title: NSLocalizedString("miles", comment: ""), style: .default, handler: {action inprint("點擊了英里")cell.detailTextLabel?.text = NSLocalizedString("miles", comment: "")})alertController.addAction(cancelAction)alertController.addAction(kmAction)alertController.addAction(milesAction)self.present(alertController, animated: true, completion: nil)}3.其他舉例
3.1 下面只有一個確定按鈕(帶截圖和代碼)
func haveBindAlert(){let alertController = UIAlertController(title: "設備已經被綁定",message: nil, preferredStyle: .alert)let okAction = UIAlertAction(title: "確定", style: .cancel, handler: nil)alertController.addAction(okAction)self.present(alertController, animated: true, completion: nil)}3.2 只有代碼
let alertController = UIAlertController(title: "需要定位權限",message: "用于提高精確的定位及導航服務", preferredStyle: .alert)let cancelAction = UIAlertAction(title: "暫不設置", style: .cancel, handler: nil)let okAction = UIAlertAction(title: "去設置", style: .default, handler: {action inprint("點擊了確定")//跳到定位設置})alertController.addAction(cancelAction)alertController.addAction(okAction)self.present(alertController, animated: true, completion: nil)4.參考博客
上圖的效果參考下面這個博客:
UIAlertController使用
參考博客:
Swift UIAlertController的用法
相關博客:
ios原生彈框UIAlertController(oc版)
總結
以上是生活随笔為你收集整理的ios swift5 弹出原生的提示框(弹框) UIAlertController的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 电大通用计算机考试,电大计算机考试试题
- 下一篇: 关于单片机串口接收不固定字节长度的解决方