内容简介:翻译自:https://stackoverflow.com/questions/30286395/uialertcontroller-uialertaction-tag-userdata-or-anything-in-swift
字典中的Names:
[ { "Name": "Doctor for Disease AAA", "Doctor_id": "21" }, { "Name": "Doctor for Disease BBB", "Doctor_id": "22" }, { "Name": "Doctor for Disease AAA", "Doctor_id": "25" } ]
因此,在按钮单击委托时,我可以获取按钮索引并可以获取相应的“名称”和“Doctor_id”.这工作正常.
但现在好像’UIActionSheet’已被弃用,我必须使用’UIAlertController’.因为我有一个大数据,我正在遍历我的数组值并调用alertcontroller处理程序(所以单击所有按钮的单个函数).但是如何从UIAlertController获取按钮索引,以便我可以同时获取“Name”和“Doctor_id”.
请帮我.
你有多种可能性.
您可以使用find来获取UIAlertAction索引
find让你找到数组中对象的索引.您可以使用它在所有操作的alert.actions数组中查找操作的索引(作为UIAlertAction的处理程序的参数传递,它是UIAlertAction本身).
let alert = UIAlertController(title: "Doctors", message: "Choose a doctor", preferredStyle: .ActionSheet) let closure = { (action: UIAlertAction!) -> Void in let index = find(alert.actions as! [UIAlertAction], action) println("Index: \(index)") } alert.addAction(UIAlertAction(title: "Doc1", style: .Default, handler: closure)) alert.addAction(UIAlertAction(title: "Doc2", style: .Default, handler: closure)) alert.addAction(UIAlertAction(title: "Doc3", style: .Default, handler: closure)) alert.addAction(UIAlertAction(title: "Doc4", style: .Default, handler: closure)) alert.addAction(UIAlertAction(title: "Cancel", style: .Cancel) { _ in println("User cancelled.") }) self.presentViewController(alert, animated: true) {}
您可以创建一个闭包…返回一个闭包
创建一个闭包,它接受您选择的参数(这里是一个Int)并返回一个捕获该参数的闭包,以便您可以使用它
let alert = UIAlertController(title: "Doctors", message: "Choose a doctor", preferredStyle: .ActionSheet) let closure = { (index: Int) in { (action: UIAlertAction!) -> Void in println("Index: \(index)") } } alert.addAction(UIAlertAction(title: "Doc1", style: .Default, handler: closure(0))) alert.addAction(UIAlertAction(title: "Doc2", style: .Default, handler: closure(1))) alert.addAction(UIAlertAction(title: "Doc3", style: .Default, handler: closure(2))) alert.addAction(UIAlertAction(title: "Doc4", style: .Default, handler: closure(3))) alert.addAction(UIAlertAction(title: "Cancel", style: .Cancel) { _ in println("User cancelled.") }) self.presentViewController(alert, animated: true) {}
这样你就有了一个为你的UIAlertAction处理程序生成闭包的函数(闭包),它们都具有相同的主体,除了它们捕获一个不同的对象(这里是一个不同的Int).
这个解决方案的真正优点是你可以捕获任何东西.您甚至可以捕获代表您的医生的假设医生对象,或直接捕获医生ID等!
使用循环
但通常你会使用for循环添加你的动作,那么为什么不利用它,加上利用闭包和它们捕获变量的事实,做一个很好的功能,直接告诉你所选医生的ID?
func testMyAlert() { let doctors = [ ["Name": "Doctor for Disease AAA", "Doctor_id": "21"], ["Name": "Doctor for Disease BBB", "Doctor_id": "22"], ["Name": "Doctor for Disease AAA", "Doctor_id": "25"] ] chooseDoctor(doctors) { selectedDocID in if let docID = selectedDocID { println("User selected doctor with ID \(docID)") } else { println("User cancelled, no doctor selected") } } } func chooseDoctor(doctors: Array<[String:String]>, completion: Int?->Void) { let alert = UIAlertController(title: "Doctors", message: "Choose a doctor", preferredStyle: .ActionSheet) for doc in doctors { let action = UIAlertAction(title: doc["Name"]!, style: UIAlertActionStyle.Default) { _ in // On selecting this action, get the doctor's ID, convert it to an Int, and return that. completion(doc["Doctor_id"]?.toInt()) } alert.addAction(action) } alert.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel) { _ in completion(nil) } ) self.presentViewController(alert, animated: true) {} }
翻译自:https://stackoverflow.com/questions/30286395/uialertcontroller-uialertaction-tag-userdata-or-anything-in-swift
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:- 让 AI “读懂” 短视频,爱奇艺内容标签技术解析
- 内容管理系统 CLTPHP 5.7 发布,自定义标签变化
- 内容管理系统 CLTPHP 5.7 发布,自定义标签变化
- HTML5常用标签(2-4)链接标签及多媒体标签
- 基于标签特定文本表示的文本多标签分类
- HTML5常用标签(2-3)图片标签
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
XML Hacks
Michael Fitzgerald / O'Reilly Media, Inc. / 2004-07-27 / USD 24.95
Developers and system administrators alike are uncovering the true power of XML, the Extensible Markup Language that enables data to be sent over the Internet from one computer platform to another or ......一起来看看 《XML Hacks》 这本书的介绍吧!