在 SwiftUI 中根据不同情况创建按钮
In SwiftUI creating buttons as per different cases
我有错误的 swiftUI 场景。
该错误可用于网络、登录等任何内容。
该屏幕上的按钮及其操作因错误类型而异。
enum ErrorItem: String {
case subscriptionError = "Subscription"
case networkError = "NetworkError"
}
init(errorType: ErrorItem) {
super.init()
if errorType == ErrorItem.subscriptionError {
titleString = "Login Error"
numOfButton = 1 // I do not want to use this . I want to create some closure and enum of buttons and create array of buttons
}
else {
titleString = "Something Went Wrong"
numOfButton = 2
}
}
我正在处理“numofButtons”显示的按钮数量。
但是对于每个案例的按钮数量,它们的标题和它们的动作也会不同。
处理该问题的最佳方法是什么?
我可以创建这样的东西并拥有 ErrorButtonTypes 数组吗?因此,如果一个屏幕包含 2 个按钮,即关闭按钮和操作按钮,我可以同时使用这两个按钮,如果一个屏幕包含 1 个按钮,仅关闭按钮,我只能使用 1 个按钮
enum ErrorButtonType {
case close(String) // String = title
case action(String, (() -> Void) // String = title, function = action to take with that button
}
在枚举中声明 var 并为每种情况设置值的简单有效的方法。像这样
enum ErrorItem: String {
case subscriptionError = "Subscription"
case networkError = "NetworkError"
var title: String {
switch self {
case .subscriptionError:
return "Login Error"
case .networkError:
return "Something Went Wrong"
}
}
var numOfButton: Int {
switch self {
case .subscriptionError:
return 1
case .networkError:
return 2
}
}
}
你的初始化是
init(errorType: ErrorItem) {
super.init()
titleString = errorType.title
numOfButton = errorType.numOfButton
}
编辑
根据您编辑的问题,您可以将枚举与这样的操作一起使用。
你的枚举
enum ErrorButtonType {
case close(String) // String = title
case action(String, (() -> Void)) // String = title, function = action to take with that button
}
声明你的枚举
var errorType: ErrorButtonType = ErrorButtonType.close("Close")
设置枚举值
let action: (() -> Void) = {
// Your button action
}
errorType = ErrorButtonType.action("Action", action)
获取枚举值
switch errorType {
case .close(let title):
print(title)
case .action(let title, let action):
print(title)
action() // Assign this action to button
}
我有错误的 swiftUI 场景。 该错误可用于网络、登录等任何内容。 该屏幕上的按钮及其操作因错误类型而异。
enum ErrorItem: String {
case subscriptionError = "Subscription"
case networkError = "NetworkError"
}
init(errorType: ErrorItem) {
super.init()
if errorType == ErrorItem.subscriptionError {
titleString = "Login Error"
numOfButton = 1 // I do not want to use this . I want to create some closure and enum of buttons and create array of buttons
}
else {
titleString = "Something Went Wrong"
numOfButton = 2
}
}
我正在处理“numofButtons”显示的按钮数量。 但是对于每个案例的按钮数量,它们的标题和它们的动作也会不同。 处理该问题的最佳方法是什么?
我可以创建这样的东西并拥有 ErrorButtonTypes 数组吗?因此,如果一个屏幕包含 2 个按钮,即关闭按钮和操作按钮,我可以同时使用这两个按钮,如果一个屏幕包含 1 个按钮,仅关闭按钮,我只能使用 1 个按钮
enum ErrorButtonType {
case close(String) // String = title
case action(String, (() -> Void) // String = title, function = action to take with that button
}
在枚举中声明 var 并为每种情况设置值的简单有效的方法。像这样
enum ErrorItem: String {
case subscriptionError = "Subscription"
case networkError = "NetworkError"
var title: String {
switch self {
case .subscriptionError:
return "Login Error"
case .networkError:
return "Something Went Wrong"
}
}
var numOfButton: Int {
switch self {
case .subscriptionError:
return 1
case .networkError:
return 2
}
}
}
你的初始化是
init(errorType: ErrorItem) {
super.init()
titleString = errorType.title
numOfButton = errorType.numOfButton
}
编辑
根据您编辑的问题,您可以将枚举与这样的操作一起使用。 你的枚举
enum ErrorButtonType {
case close(String) // String = title
case action(String, (() -> Void)) // String = title, function = action to take with that button
}
声明你的枚举
var errorType: ErrorButtonType = ErrorButtonType.close("Close")
设置枚举值
let action: (() -> Void) = {
// Your button action
}
errorType = ErrorButtonType.action("Action", action)
获取枚举值
switch errorType {
case .close(let title):
print(title)
case .action(let title, let action):
print(title)
action() // Assign this action to button
}