日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

ios swift5 弹出原生的提示框(弹框) UIAlertController

發布時間:2023/12/31 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。