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. 风格类型: ...
随机推荐
- C#开发Office程序
标题:Office 解决方案开发概述 (VSTO) 地址:https://docs.microsoft.com/zh-cn/visualstudio/vsto/office-solutions-dev ...
- apache/tomcat笔记
apache是什么? apache http server 简称apache是世界上排名前列的web服务器,因开源,简单,高性能,速度快,还可以做代理服务器,所以广受人们欢迎 httpd:httpd是 ...
- GIS 基础知识简介
前言 前一段时间,在公司进行了分析 GIS 基础信息的介绍.之所以会有这个介绍以及为什么是我?这个个中缘由说下. 公司不是一个GIS方面的公司,但是由于业务的需要,经常需要用到地图(要和地图打交道), ...
- Selenium(十二)嵌套frame定位
第一种:iframe有id.name属性 网页上有3个frame:header.menu.main,分别代码顶部.左侧.右侧(其中menu.main在另外一个frameset中) 如何定位到“head ...
- (一)AppScan的安装及破解
IBM AppScan是一款目前最好用的Web 应用安全测试工具,Rational AppScan 可自动化 Web 应用的安全漏洞评估工作,能扫描和检测所有常见的 Web 应用安全漏洞,例如 SQL ...
- python----装饰器(几种常见方式的使用与理解)
更详细的装饰器,真心实力推荐,里面介绍的很清楚,介绍可见链接:https://blog.csdn.net/buster_zr/article/details/81104551 1.装饰器的理论: (1 ...
- latex 表格每行设置不同字体
Each cell of a table is set in a box, so that a change of font style (or whatever) only lasts to the ...
- concat以及group_concat的用法
concat()函数 1.功能:将多个字符串连接成一个字符串. 2.语法:concat(str1, str2,...) 返回结果为连接参数产生的字符串,如果有任何一个参数为null,则返回值为null ...
- 2018 南京网络预赛Sum ——莫比乌斯反演
题意 设 $f(n)$ 为 $n=ab$ 的方案数,其中 $a,b$ 为无平方因子数.求 $\displaystyle \sum_{i=1}^nf(i)$,$n \leq 2e7$. 分析 显然,可 ...
- python中的函数、生成器的工作原理
1.python中函数的工作原理 def foo(): bar() def bar(): pass python的解释器,也就是python.exe(c编写)会用PyEval_EvalFramEx(c ...