如何获取在UIAlertController Swift的Alert中选择的项目的索引

use*_*509 2 indexing swift uialertcontroller

我将使用UIAlertController为用户选择一个项目.要选择的项目如下:

let arraySelect = ["NewYork", "Washington", "Seoul", "Tokyo", "Peking", "Sidney", ... ]

   let alert = UIAlertController(title: nil, message: nil, preferredStyle: .Alert)

        // Add items in array to Alert
        for var i = 0; i < arraySelect.count; i++ {
            alert.addAction(UIAlertAction(title: arrayBibleVersions[i], style: .Default, handler: {(_) in }))
        }

        // Add cancel button.    
        alert.addAction(UIAlertAction(title: "??", style: .Cancel, handler: {(_) in }))

        self.presentViewController(alert, animated: false, completion: nil)
Run Code Online (Sandbox Code Playgroud)

当用户触摸一个项目时,我必须获取用户触摸的项目的索引.但我不知道如何获得索引..请帮帮我.

use*_*509 8

我解决了我的问题如下:

    let arraySelect = ["NewYork", "Washington", "Seoul", "Tokyo", "Peking", "Sidney", ... ]

    let alert = UIAlertController(title: nil, message: nil, preferredStyle: .Alert)

    let closure = { (action: UIAlertAction!) -> Void in
        let index = alert.actions.indexOf(action)

        if index != nil {
            NSLog("Index: \(index!)")
        }
    }

    for var i = 0; i < arrayBibleVersions.count; i++ {
        alert.addAction(UIAlertAction(title: arrayBibleVersions[i][1], style: .Default, handler: closure))
    }

    alert.addAction(UIAlertAction(title: "cancel", style: .Cancel, handler: {(_) in }))

    self.presentViewController(alert, animated: false, completion: nil)
Run Code Online (Sandbox Code Playgroud)