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

Tha*_*ran 7 ios swift uialertcontroller

在我的iOS动作表中,我显示了JSON字典中的Names:

[
  { "Name": "Doctor for Disease AAA",
    "Doctor_id": "21"
  },
  { "Name": "Doctor for Disease BBB",
    "Doctor_id": "22"
  },
  { "Name": "Doctor for Disease AAA",
    "Doctor_id": "25"
  }
]
Run Code Online (Sandbox Code Playgroud)

因此,在按钮单击委托时,我可以获取按钮索引并可以获取相应的"名称"和"Doctor_id".这工作正常.

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

请帮我.

Ali*_*are 15

你有多种可能性.

您可以使用find获取UIAlertAction索引

find让您找到数组中对象的索引.您可以将它用于所有操作数组中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) {}
Run Code Online (Sandbox Code Playgroud)

您可以创建一个返回闭包的闭包

创建一个闭包,它接受您选择的参数(此处为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) {}
Run Code Online (Sandbox Code Playgroud)

这样你就有了一个为你的UIAlertAction处理程序生成闭包的函数(闭包),它们都具有相同的主体,除了它们捕获不同的对象(Int这里不同).

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

使用循环

但通常你会使用一个for循环添加你的动作,那么为什么不利用它,加上利用闭包和它们捕获变量的事实,做一个很好的功能,直接告诉你所选医生的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) {}

}
Run Code Online (Sandbox Code Playgroud)


use*_*509 5

您可以按如下方式解决问题:

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)


kan*_*aya 0

我建议你在需要的时候通过闭包来调用。它是按需的。也许这样的事情会有所帮助。

var alert = UIAlertController(title: "Title", message: "do this", preferredStyle: UIAlertControllerStyle.ActionSheet)
    var action = UIAlertAction(title: "Doc1", style: UIAlertActionStyle.Default) { (action) -> Void in
        //do some work here
    }
Run Code Online (Sandbox Code Playgroud)

但同样,我猜你在操作表上显示的项目不会超过 4 个,因为这不利于设计。关闭可能是个好主意。如果我做错了,建议我,我很乐意帮助你。干杯。