【问题标题】:How to get the index of item selected in Alert of UIAlertController Swift如何获取在 UIAlertController Swift 的警报中选择的项目的索引
【发布时间】:2015-11-26 06:07:23
【问题描述】:

我将使用 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)

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

【问题讨论】:

    标签: swift indexing uialertcontroller


    【解决方案1】:

    我解决了我的问题如下:

        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)
    

    【讨论】:

    • 不幸的是,这仅在您可以保证字符串是唯一的情况下才有效。在我的场景中,我允许人们按名字选择家庭成员。当父母和孩子的名字相同时,就会出现问题。
    • 作为一种解决方法,我将不得不在标题中添加一个键,直到我能想到更好的解决方案。 “凯文(1234)”,“凯文(2311)”,“桑德拉(5122)”等
    • 它需要在 swift 5 中改变 :let index = alert.actions.indexOf(action) -> let index = alert.actions.firstIndex(of: action)
    【解决方案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])
                }
            })
        )
    }
    

    【讨论】:

      【解决方案3】:

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

      但注意如果数组中的项相同,它将返回第一项的索引。

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

      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】:

        这很好用:

        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)
        

        【讨论】:

          【解决方案5】:

          当用户选择一个动作时执行块。

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

          【讨论】:

          • 他正在询问获取所选项目的索引。他已经写好了block的代码。
          【解决方案6】:
          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)
          }
          

          【讨论】:

          • 他正在询问获取所选项目的索引。他已经实现了你写的代码:/
          猜你喜欢
          • 2013-03-23
          • 1970-01-01
          • 2019-03-09
          • 1970-01-01
          • 2015-12-04
          • 1970-01-01
          • 2021-07-08
          • 1970-01-01
          • 2010-09-16
          相关资源
          最近更新 更多