UIAlertController'UIAlertAction'标签/用户数据或Swift中的任何内容 [英] UIAlertController 'UIAlertAction' tag/userdata or anything in Swift

查看:138
本文介绍了UIAlertController'UIAlertAction'标签/用户数据或Swift中的任何内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的iOS动态表中,我显示了JSON字典中的名字:

in my iOS actionsheet, I am showing Names from the JSON dictionary:

[
  { "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'。这个工作正常。

So, on button click delegate, I can get the button index and can fetch the corresponding 'Name' and 'Doctor_id'. This is working fine.

但现在好像'UIActionSheet'已被弃用,我必须使用'UIAlertController'。因为我有一个大数据,我正在迭代我的数组值并调用alertcontroller处理程序(所以单击所有按钮的单个函数)。但是如何从UIAlertController获取按钮索引,以便我可以同时获取Name和Doctor_id。

But now it seems like 'UIActionSheet' is deprecated, and I have to use 'UIAlertController'. As I have a large data, I am iterating through my array values and calling the alertcontroller handler (so a single function for all button click). But how can I get the button index from UIAlertController, so that I can fetch the 'Name' and 'Doctor_id' simultaneously.

请帮助我。

推荐答案

这里有多种可能性。

find 让您找到数组中对象的索引。您可以使用查找 操作的索引(作为<$ c $的参数传递) c> UIAlertAction 的处理程序, alert.actions UIAlertAction 本身) >所有动作的数组。

find let you find the index of an object in an array. You can use it to find the index of the action (that is passed as the parameter of the UIAlertAction's handler, which is the UIAlertAction itself) in the alert.actions array of all actions.

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 )并返回一个捕获该参数的闭包,以便你可以使用它

You can create a closure… that returns a closure

Create a closure that takes a parameter of your choice (here an Int) and return a closure that captures that parameter so you can use it

 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 )之外,它们都具有相同的主体。

This way you have a function (closure) that generate closures for your UIAlertAction handlers, all with the same body except that they capture a different object (a different Int here).

这个解决方案的真正优点是你可以捕获任何东西。您甚至可以捕获代表您的医生的假设医生对象,或直接捕获医生ID等。

What is really great with this solution is that you can capture anything. You can even capture an hypothetic Doctor object that represent your doctor, or directly the doctor ID, etc.!

但通常你会使用循环添加你的动作,所以为什么不利用它,加上利用关闭和捕获变量的事实,做一个很好的功能,直接告诉你所选医生的ID?

But generally you will add your actions using a for loop, so why not take advantage of that, plus take advantage of closure and the fact that they capture variables, to make a nice function that will directly tell your the selected doctor's 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) {}

}

这篇关于UIAlertController'UIAlertAction'标签/用户数据或Swift中的任何内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆