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.
} }
效果

Swift_IOS之提示框UIAlertController的更多相关文章
- 选择提示框UIAlertController 和网络状态判断AFNetworking
// 选择提示框 DownloadView *vc = [[DownloadView alloc] initWithFrame:CGRectMake(, , SCREEN_WIDTH, SCREEN_ ...
- iOS -iOS9中提示框(UIAlertController)的常见使用
iOS 8 之前提示框主要使用 UIAlertView和UIActionSheet:iOS 9 将UIAlertView和UIActionSheet合二为一为:UIAlertController . ...
- 19. UIAlertController 提示框获取文本内容,打印控制台上
1.首先定义一个全局字符串变量,方便接收获取的文本内容 2. -(void)viewDidAppear:(BOOL)animated{ UIAlertController * alert = [UIA ...
- 提示框(UIAlertController)的使用。
添加出现在屏幕中间的提示框(也就是之前的UIAlertView): UIAlertController * av = [UIAlertController alertControllerWithTit ...
- Swift - 告警提示框(UIAlertController)的用法
自iOS8起,苹果就建议告警框使用UIAlertController来代替UIAlertView.下面总结了一些常见的用法: 1,简单的应用(同时按钮响应Handler使用闭包函数) 1 2 3 ...
- iOS - UIAlertController三种显示提示框代码
UIAlertView在IOS 8以上版本已经过时了,官方推荐我们使用UIAlertController代替UIAlertView.UIActionSheet 1、UIAlertController显 ...
- iOS开发——UI基础-提示框
提示框的种类有很多,废话不多说,直接上代码 一.文本提示框 运行结果如下: 代码实现如下: @interface ViewController () // 添加方法 - (IBAction)add; ...
- WKWebView不显示提示框(Swift)
使用WKWebView的时候会出现明明自己做的一些页面有提示框, 为什么使用别人的页面提示框总是不显示, 其实很大部分原因是因为该提示框是通过JS调用的, 需要实现WKUIDelegate来进行监听 ...
- iOS:提示框(警告框)控件UIActionSheet的详解
提示框(警告框)控件2:UIActionSheet 功能:当点击按钮或标签等时,弹出一个提示框,显示必要的提示,然后通过添加的按钮完成需要的功能.它与导航栏类似,它继承自UIView. 风格类型: ...
随机推荐
- rhel7下安装EPEL源
1.rhel7安装aliyun下的epel源 wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
- IOTA私有链简单搭建
IOTA 参考:https://github.com/iotaledger/wallet 参考:https://github.com/iotaledger/iota.js 参考:https://git ...
- Sql Server 2017 安装问题记录
记录了我在虚拟机中安装Sql server 2017遇到的一些问题. 安装环境: Sql server 2017 + Windows Server 2012 R2 提供两个网上的下载链接: https ...
- 跨域访问支持(Spring Boot、Nginx、浏览器)
原文:http://www.itmuch.com/work/cors/ 最近家中事多,好久没有写点啥了.一时间竟然不知从何说起.先说下最近家里发生的事情吧: 老爸肺气肿住院: 老妈甲状腺囊肿 儿子喘息 ...
- Eclipse启动tomcat超时
启动tomcat 超时 Server Tomcat v8.0 Server at localhost was unable to start within 45 seconds. If the ser ...
- SQL Server 2005的几个新功能
SQL Server 2005相对于SQL Server 2000改进很大,有些还是非常实用的. 举几个例子来简单说明 这些例子我引用了Northwind库. 1. TOP 表达式 SQL Serv ...
- POJ 2155 Matrix[树状数组+差分]
原题链接:https://vjudge.net/problem/POJ-2155 题目大意 给定 n* n 矩阵A,其元素为0或1. A [i][j] 表示第i行和第j列中的数字.最初全为0. 我们有 ...
- 运维CMDB建设思路
在我们日常的运维工作中,面对着大量的基础设施和软件服务,该如何管理?这个管理的原则又是什么?粒度该如何控制?我们是否可以建立一个统一的标准模型来管理以上对象?管理过程中,如何降低人力成本?资源对象的生 ...
- CSS定位有几种?分别描述其不同
static(静态定位):即默认值,元素框正常生成的,top.right.bottom.left这些偏移属性不会影响静态定位的正常显示(属性不应用): relative(相对定位):元素相对自身偏移某 ...
- vue 用户登录 路由拦截 vuex cookie
功能: // 页面跳转后发送ajax请求给后端 请求详细信息 //点击课程推荐跳转到推荐课程详细 // 页面刚加载立即执行函数 = mounted <template> <div&g ...