zoukankan      html  css  js  c++  java
  • [Xcode 实际操作]四、常用控件-(9)普通警告窗口的使用

    目录:[Swift]Xcode实际操作

    本文将演示警告窗口的使用方法。

    警告窗口不仅可以给用户展现提示信息,还可以提供若干选项供用户选择。

    在项目导航区,打开视图控制器的代码文件【ViewController.swift】

     1 import UIKit
     2 
     3 class ViewController: UIViewController {
     4 
     5     override func viewDidLoad() {
     6         super.viewDidLoad()
     7         // Do any additional setup after loading the view, typically from a nib.
     8         //首先创建一个按钮,当点击按钮时,弹出警告窗口。
     9         let bt = UIButton(type: UIButton.ButtonType.system)
    10         //设置按钮的位置为(20,120),尺寸为(280,44)
    11         bt.frame = CGRect(x: 20, y: 120,  280, height: 44)
    12         //接着设置按钮在正常状态下的标题文字
    13         bt.setTitle("Question", for: .normal)
    14         //为按钮绑定点击事件
    15         bt.addTarget(self, action: #selector(ViewController.showAlert), for: .touchUpInside)
    16         //设置按钮的背景颜色为浅灰色
    17         bt.backgroundColor = UIColor.lightGray
    18         //将按钮添加到当前视图控制器的根视图
    19         self.view.addSubview(bt)
    20     }
    21     
    22     //创建一个方法,用来响应按钮的点击事件
    23     @objc func showAlert()
    24     {
    25         //初始化一个警告窗口并设置窗口的标题文字和提示信息
    26         let alert = UIAlertController(title: "Information", message: "Are you a student?", preferredStyle: UIAlertController.Style.alert)
    27         
    28         //创建一个按钮,作为提示窗口中的提示按钮。
    29         //当用户点击此按钮时,在控制台打印输出日志
    30         let yes = UIAlertAction(title: "Yes", style: UIAlertAction.Style.default, handler: {(alerts: UIAlertAction) -> Void in print("Yes, I'm a student.")})
    31         
    32         //再创建一个按钮,作为提示窗口中的提示按钮。
    33         //当用户点击此按钮时,在控制台打印输出日志
    34         let no = UIAlertAction(title: "No", style: UIAlertAction.Style.default, handler: {(alerts: UIAlertAction) -> Void in print("No, I'm not a student.")})
    35         
    36         //将两个按钮,分别添加到提示窗口
    37         alert.addAction(yes)
    38         alert.addAction(no)
    39         
    40         //在当前视图控制器中,展示提示窗口
    41         self.present(alert, animated: true, completion: nil)
    42     }
    43 
    44     override func didReceiveMemoryWarning() {
    45         super.didReceiveMemoryWarning()
    46         // Dispose of any resources that can be recreated.
    47     }
    48 }
  • 相关阅读:
    前端
    pymysql
    Linux下PostgreSQL 的安装与配置
    PostgreSQL 连接问题 FATAL: no pg_hba.conf entry for host
    PostgreSQ 连接问题 FATAL: no pg_hba.conf entry for host
    中文环境下PostgreSQL的使用
    初学者遇到的PostgreSQL字符集问题的解决
    PostgreSQL 图形化客户端工具有哪些
    PostgreSQL 图形化客户端工具的使用技巧你都get了吗?
    PostgreSQL新手教程
  • 原文地址:https://www.cnblogs.com/strengthen/p/10016184.html
Copyright © 2011-2022 走看看