swift 学习之 UIAlertViewController

本文介绍了一个iOS应用中使用UIAlertController创建提示框和操作表的具体实现方式,包括不同样式的警告框及带有输入框的登录对话框。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//
//  PushViewController.swift
//  tab
//
//  Created by su on 15/12/7.
//  Copyright © 2015年 tian. All rights reserved.
//

import UIKit

class PushViewController: UIViewController {
    var alert1: UIAlertController!
    var alert2: UIAlertController!
    var actionSheet: UIAlertController!
   
    override func viewDidLoad() {
        super.viewDidLoad()
//        label.frame = CGRect(x: 20, y: 100, width: Tools().SCREEN_WIDTH, height: 100)
//        label.backgroundColor = Tools().RGB(r: 123, g: 234, b: 132)
       
       
        // 定义一个按钮,显示最简单的 Alert
        let button1 = UIButton(type: UIButtonType.System)
        button1.frame = CGRectMake(self.view.frame.width/2 - 200, 50, 400, 50)
        button1.setTitle("最简单的 Alert", forState: UIControlState.Normal)
        button1.addTarget(self, action: "buttonAction:", forControlEvents: UIControlEvents.TouchUpInside)
        button1.tag = 1
        self.view.addSubview(button1)
       
       
        // 定义一个按钮,显示带文本框的 Alert
        let button2 = UIButton(type: UIButtonType.System)
        button2.frame = CGRectMake(self.view.frame.width/2 - 200, 150, 400, 50)
        button2.setTitle("带文本框的 Alert", forState: UIControlState.Normal)
        button2.addTarget(self, action: "buttonAction:", forControlEvents: UIControlEvents.TouchUpInside)
        button2.tag = 2
         self.view.addSubview(button2)
        // 定义一个按钮,显示上拉菜单
        let button3 = UIButton(type: UIButtonType.System)
        button3.frame = CGRectMake(self.view.frame.width/2 - 200, 250, 400, 50)
        button3.setTitle("上拉菜单", forState: UIControlState.Normal)
        button3.addTarget(self, action: "buttonAction:", forControlEvents: UIControlEvents.TouchUpInside)
        button3.tag = 3
        self.view.addSubview(button3)

        // 定义 cancel、ok、save、delete、reset 的 UIAlertAction
        let cancelAction = UIAlertAction(title: "cancel", style: UIAlertActionStyle.Cancel, handler: nil)
        let okAction = UIAlertAction(title: "ok", style: UIAlertActionStyle.Default){
            (action: UIAlertAction!) -> Void in
            print("you choose ok")
        }
        let saveAction = UIAlertAction(title: "save", style: UIAlertActionStyle.Default){
            (action: UIAlertAction!) -> Void in
            print("you choose save")
        }
        let deleteAction = UIAlertAction(title: "delete", style: UIAlertActionStyle.Destructive){
            (action: UIAlertAction!) -> Void in
            print("you choose delete")
        }
        let resetAction = UIAlertAction(title: "reset", style: UIAlertActionStyle.Destructive){
            (action: UIAlertAction!) -> Void in
            print("you choose reset")
        }
       
       
        // 1、初始化最简单的 Alert
        alert1 = UIAlertController(title: "simple alert", message: "this is a simple alert", preferredStyle: UIAlertControllerStyle.Alert)
        alert1.addAction(cancelAction)
        alert1.addAction(resetAction)
        alert1.addAction(okAction)
       
        // 2、初始化带文本框的 Alert
        alert2 = UIAlertController(title: "login alert", message: "please enter your name and password", preferredStyle: UIAlertControllerStyle.Alert)
        alert2.addTextFieldWithConfigurationHandler {
            (textField: UITextField!) -> Void in
            textField.placeholder = "name"
        }
        alert2.addTextFieldWithConfigurationHandler {
            (textField: UITextField!) -> Void in
            textField.placeholder = "password"
            textField.secureTextEntry = true
        }
        let loginAction = UIAlertAction(title: "login", style: UIAlertActionStyle.Default) {
            (action: UIAlertAction!) -> Void in
            let name = self.alert2.textFields!.first
            let password = self.alert2.textFields!.last
            print("name : \(name!.text) password : \(password!.text)")
        }
        alert2.addAction(loginAction)
       
        // 3、初始化上拉菜单
        actionSheet = UIAlertController(title: "simple action sheet", message: "action sheet message", preferredStyle: UIAlertControllerStyle.ActionSheet)
        actionSheet.addAction(cancelAction)
        actionSheet.addAction(deleteAction)
        actionSheet.addAction(saveAction)
       
     
    }
   
    /// 按钮响应事件
    func buttonAction(sender: UIButton) {
        let num = sender.tag
        switch num {
        case 1:
            self.presentViewController(alert1, animated: true, completion: nil)
        case 2:
            self.presentViewController(alert2, animated: true, completion: nil)
        case 3:
            self.presentViewController(actionSheet, animated: true, completion: nil)
        default:
            break
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
   

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */
}

转载于:https://www.cnblogs.com/tian-sun/p/5028471.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值