'SwiftUI - Dynamic list of buttons in ActionSheet
I need to generate a dynamic list of buttons in an ActionSheet
. Say I have an array of options ["Option1", "Option2"]
, how can I achieve this?
.actionSheet(isPresented: self.$showSheet, content: {
ActionSheet(title: Text("Select an option"), buttons: [
.default(Text("Option1")){self.option = 1},
.default(Text("Option2")){self.option = 2},
.default(Text("Option3")){self.option = 3},
.default(Text("Option4")){self.option = 4},
.cancel()])
}
Solution 1:[1]
Here is possible solution.
Update: Xcode 13.3 / iOS 15.4
Now actionSheet
is replaced with confirmationDialog
which accepts view builder, so now it is just doable inside, like
.confirmationDialog("", isPresented: $showSheet) {
ForEach(currentOptions.indices, id: \.self) { i in
Button(currentOptions[i]) { self.option = i + 1 }
}
}
Deprecated:
Tested with Xcode 11.4 / iOS 13.4
having helper function
func generateActionSheet(options: [String]) -> ActionSheet {
let buttons = options.enumerated().map { i, option in
Alert.Button.default(Text(option), action: { self.option = i + 1 } )
}
return ActionSheet(title: Text("Select an option"),
buttons: buttons + [Alert.Button.cancel()])
}
you then can use
.actionSheet(isPresented: self.$showSheet, content: {
// assuming you have `currentOptions` (or similar) property for dynamic
// options
self.generateActionSheet(options: self.currentOptions)
})
Solution 2:[2]
You can achieve with something similar to below;
@State var flag: Bool = false
@State var options: [(String, () -> Void)] = [
("Option - 1", { print("option1 selected")}),
("Option - 2", { print("option2 selected")}),
("Option - 3", { print("option3 selected")})
]
var body: some View {
Button("Show action sheet") {
self.flag = true
}
.actionSheet(isPresented: self.$flag, content: {
var buttons: [ActionSheet.Button] = options.map {
ActionSheet.Button.default(Text($0.0), action: $0.1)
}
buttons.append(.cancel())
return ActionSheet(title: Text("Select an option"), buttons: buttons)
})
}
Solution 3:[3]
you can try something like this
struct ContentView: View {
@State var showActionSheet: Bool = false
var titles: [String] = ["Option1", "Option2", "Option3"]
var buttonsArray: NSMutableArray = NSMutableArray()
init() {
loadArray()
}
var body: some View {
Button("Show Action Sheet") {
self.showActionSheet = true
}.actionSheet(isPresented: $showActionSheet) { () -> ActionSheet in
ActionSheet(title: Text("Some Action"),
message: Text("optional message"),
buttons: self.buttonsArray as! [ActionSheet.Button])
}
}
func loadArray() {
for i in 0..<self.titles.count {
let button: ActionSheet.Button = .default(Text(self.titles[i])) {
print(self.titles[i])
}
self.buttonsArray[i] = button
}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Enes Karaosman |
Solution 3 | Hattori Hanz? |