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

标签 ios swift uialertcontroller

在我的 iOS 操作表中,我显示了 JSON 字典中的名称:

[
  { "Name": "Doctor for Disease AAA",
    "Doctor_id": "21"
  },
  { "Name": "Doctor for Disease BBB",
    "Doctor_id": "22"
  },
  { "Name": "Doctor for Disease AAA",
    "Doctor_id": "25"
  }
]

因此,在按钮单击委托(delegate)上,我可以获得按钮索引 并可以获取相应的“姓名”和“Doctor_id”。这工作正常。

但现在似乎“UIActionSheet”已被弃用,我必须使用“UIAlertController”。由于我有大量数据,我正在遍历我的数组值并调用警报 Controller 处理程序(因此所有按钮单击都是一个函数)。但是我怎样才能从 UIAlertController 获取按钮索引,以便我可以同时获取“姓名”和“Doctor_id”。

请帮帮我。

最佳答案

这里有多种可能性。

您可以使用find 获取UIAlertAction 索引

find 让您找到数组中对象的索引。您可以使用它来查找 action 的索引(作为 UIAlertAction 的处理程序的参数传递,它是 UIAlertAction 本身)在所有操作的 alert.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)并返回一个捕获该参数的闭包,以便您可以使用它

 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在这里)。

此解决方案的真正优点在于您可以捕获任何内容。您甚至可以捕获代表您的医生的假想 Doctor 对象,或直接捕获医生 ID 等!

使用循环

但通常您会使用 for 循环来添加您的 Action ,那么为什么不利用它,再加上利用闭包和它们捕获变量的事实,来制作一个很好的函数,它将直接告诉你选择的医生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) {}

}

关于ios - UIAlertController 'UIAlertAction' 标签/用户数据或 Swift 中的任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30286395/

相关文章:

swift - 如何将节点物理体形状设置为边界框

ios - 有没有办法减少 FBSDKCoreKit 的大小?

ios - 通过 UIAlertController 登录/注册

ios - 当呈现 actionSheet (AlertController) 时,如何修复 PopoverViewController 的边界?

ios - 添加到 NSMutableArray 时出错

ios - Asp.net MVC 网络应用程序和 iOS 移动应用程序

objective-c - iOS6 中的 TableView 圆角半径不同

带有返回字符串的iOS swift post方法

ios - 在 swift 3 xcode 8 中出现错误

ios - 如何将 UIDatePicker 添加到 UIAlertController iOS9