日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

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

發布時間:2024/7/23 java 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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生成提示框...

總結

以上是生活随笔為你收集整理的swift怎么调用Java,Swift完成UIAlertController的调用的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。