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的更多相关文章

  1. 选择提示框UIAlertController 和网络状态判断AFNetworking

    // 选择提示框 DownloadView *vc = [[DownloadView alloc] initWithFrame:CGRectMake(, , SCREEN_WIDTH, SCREEN_ ...

  2. iOS -iOS9中提示框(UIAlertController)的常见使用

    iOS 8 之前提示框主要使用 UIAlertView和UIActionSheet:iOS 9 将UIAlertView和UIActionSheet合二为一为:UIAlertController . ...

  3. 19. UIAlertController 提示框获取文本内容,打印控制台上

    1.首先定义一个全局字符串变量,方便接收获取的文本内容 2. -(void)viewDidAppear:(BOOL)animated{ UIAlertController * alert = [UIA ...

  4. 提示框(UIAlertController)的使用。

    添加出现在屏幕中间的提示框(也就是之前的UIAlertView): UIAlertController * av = [UIAlertController alertControllerWithTit ...

  5. Swift - 告警提示框(UIAlertController)的用法

    自iOS8起,苹果就建议告警框使用UIAlertController来代替UIAlertView.下面总结了一些常见的用法: 1,简单的应用(同时按钮响应Handler使用闭包函数)    1 2 3 ...

  6. iOS - UIAlertController三种显示提示框代码

    UIAlertView在IOS 8以上版本已经过时了,官方推荐我们使用UIAlertController代替UIAlertView.UIActionSheet 1、UIAlertController显 ...

  7. iOS开发——UI基础-提示框

    提示框的种类有很多,废话不多说,直接上代码 一.文本提示框 运行结果如下: 代码实现如下: @interface ViewController () // 添加方法 - (IBAction)add; ...

  8. WKWebView不显示提示框(Swift)

    使用WKWebView的时候会出现明明自己做的一些页面有提示框, 为什么使用别人的页面提示框总是不显示, 其实很大部分原因是因为该提示框是通过JS调用的, 需要实现WKUIDelegate来进行监听 ...

  9. iOS:提示框(警告框)控件UIActionSheet的详解

    提示框(警告框)控件2:UIActionSheet 功能:当点击按钮或标签等时,弹出一个提示框,显示必要的提示,然后通过添加的按钮完成需要的功能.它与导航栏类似,它继承自UIView.   风格类型: ...

随机推荐

  1. Jenkins安装Slave节点

    在系统管理----节点管理下 创建完成后会出现slave节点启动的命令,下载agent.jar,然后启动服务 Master-slave相当于Server和Agent,master管理job和slave ...

  2. apache/tomcat笔记

    apache是什么? apache http server 简称apache是世界上排名前列的web服务器,因开源,简单,高性能,速度快,还可以做代理服务器,所以广受人们欢迎 httpd:httpd是 ...

  3. 微信支付报调用支付JSAPI缺少参数: sign

    检查后台返回参数中没有paySign字段

  4. angular ionic 解决微信页面缓存问题

    # 在路由对应的页面路径后面加时间戳 .state('viewName', { url: '/viewName', cache: false, templateUrl: function(){ ret ...

  5. python_面向对象——多继承

    1.多继承 class Shenxian: def fly(self): print('神仙会飞~') class Monkey: def eat_peach(self): print('猴子喜欢吃桃 ...

  6. 301、404、200、304等HTTP状态

    在网站建设的实际应用中,容易出现很多小小的失误,就像mysql当初优化不到位,影响整体网站的浏览效果一样,其实,网站的常规http状态码的表现也是一样,Google无法验证网站几种解决办法,提及到由于 ...

  7. nginx url默认去掉index.php

  8. matlab(8) Regularized logistic regression : 不同的λ(0,1,10,100)值对regularization的影响,对应不同的decision boundary\ 预测新的值和计算模型的精度predict.m

    不同的λ(0,1,10,100)值对regularization的影响\ 预测新的值和计算模型的精度 %% ============= Part 2: Regularization and Accur ...

  9. Jquery 操作DOM元素

    一.文本输入框: text <input type=”text” value=”99.com” size=12 id=”input1” /> 1.获取文本值: $("#input ...

  10. vue2 运动及相关函数