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

时间:2015-11-26 06:07:23

标签: swift indexing 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)

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

6 个答案:

答案 0 :(得分:7)

我解决了我的问题如下:

    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)

答案 1 :(得分:2)

这就是我在Swift 4 / iOS11中解决它的方法

projects.forEach { project in
    alertController.addAction(
        UIAlertAction(title: project.name, style: .default, handler: { action in
            if let index = alertController.actions.index(where: { $0 === action }) {
                self.showProject(project: projects[index])
            }
        })
    )
}

答案 2 :(得分:1)

这很好用:

var 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 field in arraySelect {
    alert.addAction(UIAlertAction(title: field, style: .Default, handler: closure))
}

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

self.presentViewController(alert, animated: false, completion: nil)

答案 3 :(得分:0)

我也遇到同样的问题,并找到了下面的方法救了我。

但是要小心,如果数组中的项目相同,它将返回第一项的索引。

幸运的是,在这种情况下它是有效的,因为我们创建了动作并使用动作进行搜索。

extension Array where Self.Element : Equatable {

    ...

    @inlinable public func index(of element: Element) -> Int?

    ...

}

它可以在Swift 4中使用

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

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

    // Add items in array to Alert
    for name in arraySelect {
        alert.addAction(UIAlertAction(title: name, style: .default, handler: {(action) in
            // print idx that user selected
            if let idx = alert.actions.index(of: action) {
                print("idx = \(idx)")
            }

        }))
    }

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

    self.present(alert, animated: true, completion: nil)

答案 4 :(得分:-1)

当用户选择操作时执行阻止。

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

答案 5 :(得分:-2)

func FirefoxAhri(){
    let optionMenu = UIAlertController(title: "title", message: "message", preferredStyle: UIAlertControllerStyle.ActionSheet)

    let action1 = UIAlertAction(title: "Yes", style: UIAlertActionStyle.Destructive){
        action -> Void in
        //do stuff on click Yes
    }

    let action2 = UIAlertAction(title: "No", style: UIAlertActionStyle.Destructive){
        action -> Void in
        //do stuff on click No
    }

    let cancelAction: UIAlertAction = UIAlertAction(title: "Cancel", style: .Cancel) { action -> Void in
        print("Cancel")
        // do nothing if you don't want. alert closes automatically
    }

    optionMenu.addAction(action1)
    optionMenu.addAction(action2)
    optionMenu.addAction(cancelAction)
    self.presentViewController(optionMenu, animated: true, completion: nil)
}