8.UIAlertView

UIAlertView

UIAlertView可用来显示提示信息,下面我们学习如何使用它

1. UIAlertView的创建

let alertView = UIAlertView()
alertView.delegate = self
alertView.title = "摸着你的良心回答"
alertView.message = "邪恶枫叶帅吗?"
alertView.addButtonWithTitle("帅")
alertView.addButtonWithTitle("帅的冒泡")
alertView.cancelButtonIndex = 0
alertView.show()

看看运行结果就知道上面的代码的意思了,现在运行程序
UIAlert2个按钮
然后我们在加一个按钮

alertView.addButtonWithTitle("帅的天昏地暗")

运行程序
UIAlert3个按钮

感觉两个按钮似乎好看那么一丢丢

2.UIAlertView的代理

public protocol UIAlertViewDelegate : NSObjectProtocol {

    // Called when a button is clicked. The view will be automatically dismissed after this call returns
    @available(iOS, introduced=2.0, deprecated=9.0)
    optional public func alertView(alertView: UIAlertView, clickedButtonAtIndex buttonIndex: Int)

    // Called when we cancel a view (eg. the user clicks the Home button). This is not called when the user clicks the cancel button.
    // If not defined in the delegate, we simulate a click in the cancel button
    @available(iOS, introduced=2.0, deprecated=9.0)
    optional public func alertViewCancel(alertView: UIAlertView)

    @available(iOS, introduced=2.0, deprecated=9.0)
    optional public func willPresentAlertView(alertView: UIAlertView) // before animation and showing view
    @available(iOS, introduced=2.0, deprecated=9.0)
    optional public func didPresentAlertView(alertView: UIAlertView) // after animation

    @available(iOS, introduced=2.0, deprecated=9.0)
    optional public func alertView(alertView: UIAlertView, willDismissWithButtonIndex buttonIndex: Int) // before animation and hiding view
    @available(iOS, introduced=2.0, deprecated=9.0)
    optional public func alertView(alertView: UIAlertView, didDismissWithButtonIndex buttonIndex: Int) // after animation

    // Called after edits in any of the default fields added by the style
    @available(iOS, introduced=2.0, deprecated=9.0)
    optional public func alertViewShouldEnableFirstOtherButton(alertView: UIAlertView) -> Bool
}

貌似iOS9.0后UIAlertView就标记为过时了
虽然有很多方法,但是一般就用第一个.

在上面的例子的基础上, 我们实现下面的需求,点击不同的按钮答应不同的文字:
“帅” => “兄弟你的眼睛真是瞎的不行”
“帅的冒泡” => “兄弟好眼光”
“帅的天昏地暗” => “兄弟,你果然是个英雄,因为英雄所见略同”

 func alertView(alertView: UIAlertView, clickedButtonAtIndex buttonIndex: Int) {
    if buttonIndex == 0 {
        print("兄弟你的眼睛真是瞎的不行")
        return
    }

    if buttonIndex == 1 {
        print("兄弟好眼光")
        return
    }

    if buttonIndex == 2 {
        print("兄弟,你果然是个英雄,因为英雄所见略同")
        return
    }

}

3. 完整代码

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.

        let alertView = UIAlertView()
        alertView.delegate = self
        alertView.title = "摸着你的良心回答"
        alertView.message = "邪恶枫叶帅吗?"
        alertView.addButtonWithTitle("帅")
        alertView.addButtonWithTitle("帅的冒泡")
        alertView.addButtonWithTitle("帅的天昏地暗")
        alertView.cancelButtonIndex = 0
        alertView.show()

    }

    func alertView(alertView: UIAlertView, clickedButtonAtIndex buttonIndex: Int) {
        if buttonIndex == 0 {
            print("兄弟你的眼睛真是瞎的不行")
            return
        }

        if buttonIndex == 1 {
            print("兄弟好眼光")
            return
        }

        if buttonIndex == 2 {
            print("兄弟,你果然是个英雄,因为英雄所见略同")
            return
        }

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值