swift怎么调用Java,Swift完成UIAlertController的调用

Swift完成UIAlertController的调用

iOS8中的UIAlertView和UIActionSheet已经都被UIAlertViewController代替了,所以,本篇blog就来探讨下如何用swift生成提示框。

我们先来看一下Apple的UIAlertController的文档:

import Foundation

import UIKit

//

// UIAlertController.h

// UIKit

//

// Copyright (c) 2014 Apple Inc. All rights reserved.

//

@availability(iOS, introduced=8.0)

enum UIAlertActionStyle : Int {

case Default

case Cancel

case Destructive

}

@availability(iOS, introduced=8.0)

enum UIAlertControllerStyle : Int {

case ActionSheet

case Alert

}

@availability(iOS, introduced=8.0)

class UIAlertAction : NSObject, NSCopying {

convenience init(title: String, style: UIAlertActionStyle, handler: ((UIAlertAction!) -> Void)!)

var title: String { get }

var style: UIAlertActionStyle { get }

var enabled: Bool

}

@availability(iOS, introduced=8.0)

class UIAlertController : UIViewController {

convenience init(title: String?, message: String?, preferredStyle: UIAlertControllerStyle)

func addAction(action: UIAlertAction)

var actions: [AnyObject] { get }

func addTextFieldWithConfigurationHandler(configurationHandler: ((UITextField!) -> Void)!)

var textFields: [AnyObject]? { get }

var title: String?

var message: String?

var preferredStyle: UIAlertControllerStyle { get }

}

我们可以看到UIAlertController的style有两个,一个是ActionSheet,一个是Alert,而AlertActionStyle有3个: Default,Cancel, Destructive;所以我们新建Alert时可以这样:

var alert: UIAlertController = UIAlertController(title:nil, message:"您输入的电话号码有误,请检查后重新输入",

preferredStyle:UIAlertControllerStyle.Alert)

或者

var alert: UIAlertController = UIAlertController(title: nil, message:"test", preferredStyle: UIAlertControllerStyle.ActionSheet)

接下来我们来给Alert添加action,从文档中可以看到AlertAction有init函数,

我们来新建3个actions

var saveAction = UIAlertAction(title: "Save", style: .Default, handler:{

(alerts: UIAlertAction!) -> Void in

println("File saved")

})

var deleteAction = UIAlertAction(title: "Delete", style: .Default, handler:{

(alerts: UIAlertAction!) -> Void in

println("File delete")

})

var cancelAction = UIAlertAction(title: "Cancel", style: .Cancel, handler:{

(alerts: UIAlertAction!) -> Void in

println("Cancelled")

})注意到handler中用到了一个closure

然后给我们的alertcontroller添加actions,并把它显示出来

alert.addAction(saveAction)

alert.addAction(deleteAction)

alert.addAction(cancelAction)

self.presentViewController(alert, animated: true, completion: nil)

我们也可以这样添加action

alert.addAction(UIAlertAction(title: "确定", style: .Destructive, handler: {

action in switch action.style{

case .Default:

println("ok")

case .Cancel:

println("cancel")

case .Destructive:

println("Destructive")

}

}

))接下来运行一下看看我们的alertController是什么样子的吧。

Tips:

如果style是cancel 那么字体会变粗;如果是destructive,字体会显示红色。

http://www.dengb.com/Javabc/957757.htmlwww.dengb.comtruehttp://www.dengb.com/Javabc/957757.htmlTechArticleSwift完成UIAlertController的调用 iOS8中的UIAlertView和UIActionSheet已经都被UIAlertViewController代替了,所以,本篇blog就来探讨下如何用swift生成提示框...

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值