ios – UIAlertController’UIAlertAction’标签/用户数据或Swift中的任何内容

栏目: Swift · 发布时间: 5年前

内容简介:翻译自:https://stackoverflow.com/questions/30286395/uialertcontroller-uialertaction-tag-userdata-or-anything-in-swift
在我的iOS动作表中,我显示了 JSON

字典中的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


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

赢在众筹

赢在众筹

杨东、黄超达、刘思宇 / 中国经济出版社 / 2015-1-1 / 48.00

第一本股权众筹体系化图书。李克强总理要求“开展股票众筹融资试点”。人民大学杨东教授,天使街黄超达、刘思宇两位老总倾情创作。阿里巴巴、平安集团、京东等多家机构联袂推荐一起来看看 《赢在众筹》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器