• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Swift_IOS之提示框UIAlertController

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
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.
    }


}

 效果

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
[Swift]LeetCode1207.独一无二的出现次数|UniqueNumberofOccurrences发布时间:2022-07-13
下一篇:
[Swift]LCP3.机器人大冒险发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap