zoukankan      html  css  js  c++  java
  • Swift_IOS之提示框UIAlertController

    import UIKit
    
    class ViewController: UIViewController ,UIActionSheetDelegate{
        @IBAction func btn1(_ sender: UIButton) {
            label1.text="文本显示"
            let alertController = UIAlertController(title: "修改文本", message: nil, preferredStyle: UIAlertControllerStyle.alert)
            alertController.addTextField(configurationHandler: { (textField: UITextField!) -> Void in
                textField.placeholder = "请输入内容"
                // 添加监听代码,监听文本框变化时要做的操作
                NotificationCenter.default.addObserver(self, selector: #selector(self.alertTextFieldDidChange), name: NSNotification.Name.UITextFieldTextDidChange, object: textField)
            })
            let cancelAction = UIAlertAction(title: "取消", style: UIAlertActionStyle.cancel, handler: nil)
           
            let okAction = UIAlertAction(title: "确认", style: UIAlertActionStyle.default , handler: { (action: UIAlertAction!) -> Void in
                let login = (alertController.textFields?.first)! as UITextField
                let str = login.text
                self.label1.text=str
                NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UITextFieldTextDidChange, object: nil)
            })
            okAction.isEnabled = false
            alertController.addAction(cancelAction)
            alertController.addAction(okAction)
            self.present(alertController, animated: true, completion: nil)
        }
        
        /// 监听文字改变
        @objc func alertTextFieldDidChange(){
            let alertController = self.presentedViewController as! UIAlertController?
            if (alertController != nil) {
                let login = (alertController!.textFields?.first)! as UITextField
                let okAction = alertController!.actions.last! as UIAlertAction
                if (!(login.text?.isEmpty)!) {
                    okAction.isEnabled = true
                } else {
                    okAction.isEnabled = false
                }
            }
        }
        
        @IBOutlet weak var label1: UILabel!
        
        override func viewDidLoad() {
            super.viewDidLoad()
            // Do any additional setup after loading the view, typically from a nib.
        }
        @IBOutlet weak var hello: UILabel!
        
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }
    
    
    }
    

     效果图


    //
    //  ViewController.swift
    //  hello
    //
    //  Created by loaderman on 2018/12/22.
    //  Copyright © 2018年 loaderman. All rights reserved.
    //
    
    import UIKit
    
    class ViewController: UIViewController ,UIActionSheetDelegate{
        @IBAction func btn1(_ sender: UIButton) {
           
            weak var weakSelf = self // 弱引用
            let alertController = UIAlertController()
            let cancelAction = UIAlertAction(title: "取消", style: .cancel, handler: nil)
            let directMessagesAction = UIAlertAction(title: "私信", style: .default) { (action) in
                self.label1.text="私信"
            }
            let focusOnAction = UIAlertAction(title: "关注", style: .default) { (action) in
                self.label1.text="关注"
            }
            alertController.addAction(focusOnAction)
            alertController.addAction(directMessagesAction)
            alertController.addAction(cancelAction)
            weakSelf!.present(alertController, animated: true, completion: nil)
        }
        
       
        
        @IBOutlet weak var label1: UILabel!
        
        override func viewDidLoad() {
            super.viewDidLoad()
            // Do any additional setup after loading the view, typically from a nib.
        }
        @IBOutlet weak var hello: UILabel!
        
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }
    
    
    }
    

     效果

  • 相关阅读:
    android 本地字符串存取
    2020-07-17:线上一个服务有4个实例突然变得访问很慢,你会从什么地方入手找原因?
    2020-07-16:如何获得一个链表的倒数第n个元素?
    2020-07-15:死锁与活锁的区别,死锁与饥饿的区别?
    2020-07-14:es用过冷热分离吗?假如现在有些数据热变冷,有些数据冷变热,怎么解决?
    2020-07-28:已知sqrt (2)约等于 1.414,要求不用数学库,求sqrt (2)精确到小数点后 10 位。
    2020-07-29:从 innodb 的索引结构分析,为什么索引的 key 长度不能太长?
    2020-07-27:如何设计一个分布式文件系统,如何设计动态扩容和数据定位?
    2020-07-26:如何用 socket 编程实现 ftp 协议?
    2020-07-25:如何实现一个高效的单向链表逆序输出?
  • 原文地址:https://www.cnblogs.com/loaderman/p/10293571.html
Copyright © 2011-2022 走看看