Swift - 告警框(UIAlertView)的用法

1,下面代码创建并弹出一个告警框,并带有“取消”“确定”两个按钮

(注:自IOS8起,建议使用UIAlertController)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class ViewController: UIViewController{
    override func viewDidLoad() {
        super.viewDidLoad()
 
        var alertView = UIAlertView()
        alertView.title = "系统提示"
        alertView.message = "您确定要离开hangge.com吗?"
        alertView.addButtonWithTitle("取消")
        alertView.addButtonWithTitle("确定")
        alertView.cancelButtonIndex=0
        alertView.delegate=self;
        alertView.show()
    }
 
    func alertView(alertView:UIAlertView, clickedButtonAtIndex buttonIndex: Int){
        if(buttonIndex==alertView.cancelButtonIndex){
            println("点击了取消")
        }
        else
        {
            println("点击了确认")
        }
    }
}


2,告警框有下面4种样式
Default:默认样式
PlainTextInput:带输入框的告警框
SecureTextInput:带密码框的告警框
LoginAndPasswordInput:带输入框和密码框的告警框


下面是一个使用输入框和密码框的告警框样例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import UIKit
 
class ViewController: UIViewController {
 
    var alertView = UIAlertView()
     
    override func viewDidLoad() {
        super.viewDidLoad()
         
        alertView.title = "系统登录"
        alertView.message = "请输入用户名和密码!"
        alertView.addButtonWithTitle("取消")
        alertView.addButtonWithTitle("确定")
        alertView.cancelButtonIndex=0
        alertView.delegate=self;
        alertView.alertViewStyle = UIAlertViewStyle.LoginAndPasswordInput
        alertView.show()
    }
     
    func alertView(alertView:UIAlertView, clickedButtonAtIndex buttonIndex: Int){
        if(buttonIndex==alertView.cancelButtonIndex){
            println("点击了取消")
        }
        else
        {
            let name = alertView.textFieldAtIndex(0)
            let password = alertView.textFieldAtIndex(1)
            println("用户名是:\(name!.text) 密码是:\(password!.text)")
        }
    }
 
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}
posted @   brave-sailor  阅读(711)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
历史上的今天:
2014-09-25 Android 出现警告Exported service does not require permission
2013-09-25 Android中的JSON详细总结
点击右上角即可分享
微信分享提示