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. Linux学习笔记之二

    vim编辑器 :三种工作模式 vim /tmp/xueying.txt 命令模式 a.i.o/esc     \  :wq  保存并退出 /              \ 输入模式           ...

  2. VMware14虚拟机与宿主机建立通讯

    当我们在VMware14中运行虚拟机搭建实验环境就需要与我们的宿主机或另一台虚拟机连接通讯,下面我们就来看看如何建立通讯,实现虚拟机与宿主机.虚拟机与虚拟机互联互通. 准备环境:一台安装好VMware ...

  3. 《Python编程:从入门到实践》第五章 if语句 习题答案

    #5.1 major = 'Software Engineering' print("Is major =='Software Engineering'? I predict True.&q ...

  4. java项目中注解使用——整理

    文章:@Mapper注解的使用 地址:https://blog.csdn.net/weixin_39666581/article/details/81057385 @Mapper注解的的作用 1:为了 ...

  5. 模板引擎-vue中的模板如何被解析,指令如何处理

    模板是什么 <div id='app'> <div> <input v-model="title"/> <button v-on:clic ...

  6. 【python】使用openpyxl解析json并写入excel(xlsx)

    目标: 将json文本解析并存储到excel中 使用python包 openpyx import simplejsonmport codecsimport openpyxl import os # d ...

  7. WCF之MSMQ消息队列

    一.MSMQ简介 MSMQ(微软消息队列)是Windows操作系统中消息应用程序的基础,是用于创建分布式.松散连接的消息通讯应用程序的开发工具. MSMQ与XML Web Services和.Net ...

  8. 4.Python 进制和位运算

    .button, #logout { color: #333; background-color: #fff; border-color: #ccc; } span#login_widget > ...

  9. nodeJs修改镜像源

    // 设置 淘宝镜像源npm config set registry https://registry.npm.taobao.org // 查看 使用的 镜像源npm config get regis ...

  10. Java:JVM的内存模型

    JVM内存模型 JVM内存模型可以分为两个部分,如下图所示,堆和方法区是所有线程共有的,而虚拟机栈,本地方法栈和程序计数器则是线程私有的.   1. 堆(Heap) 堆内存是所有线程共有的,可以分为两 ...