如果用户点击它,我必须在视图上显示一个警报。
我的警觉取决于几种情况:
购买
。
我就是这么做的。
class AlertDialog {
enum SelectedType {
case none
case purchase
case mustBePurchased
case purchaseError
}
var selectedType:SelectedType = .none
}
struct FilteredListItem: View {
@State var showAlert: Bool = false
private var alertDialog:AlertDialog?
var body: some View {
Text(item.termLowerCase)
.font(fontItems)
.foregroundColor(.white)
.onTapGesture {
DispatchQueue.main.async {
appStoreWrapper.verifyPurchase(productID: item.package!)
{ // run if purchased
purchased = true
} runIfNotPurchased: {
purchased = false
alertDialog!.selectedType = .mustBePurchased
showAlert = true
}
}
}
.alert(isPresented: $showAlert) {
if alertDialog!.selectedType == .purchase {
appStoreWrapper.purchase(productID: item.package!) {
// run if purchased
purchased = true
} runIfPurchaseFailed: { (error) in
alertDialog!.selectedType = .purchaseError
appStoreWrapper.purchaseError = error
showAlert = true
}
} else if alertDialog!.selectedType == .purchaseError {
let primaryButton = Alert.Button.default(Text("OK")) {
showAlert = false
}
return Alert(title: Text(appStoreWrapper.makeString("ERROR")),
message: Text(appStoreWrapper.purchaseError),
dismissButton: primaryButton)
}
let dismissButton = Alert.Button.default(Text(appStoreWrapper.makeString("CANCEL"))) {
showAlert = false
}
let primaryButton = Alert.Button.default(Text("OK")) {
appStoreWrapper.purchase(productID: item.package!) {
// run if purchased
purchased = true
} runIfPurchaseFailed: { (error) in
appStoreWrapper.purchaseError = error
alertDialog!.selectedType = .purchaseError
showAlert = true
print(erro)
}
}
return Alert(title: Text(appStoreWrapper.makeString("ERROR")),
message: Text(appStoreWrapper.purchaseError),
primaryButton: primaryButton,
secondaryButton: dismissButton)
}这是我的问题:修饰符.alert(isPresented: $showAlert)期望返回一个Alert(),对吗?但是我有这些异步方法
appStoreWrapper.verifyPurchase(productID: item.package!)
{ // run if purchased },
runIfNotPurchased: { }它不能将任何内容返回给警报修饰符。我该怎么做?我做的对吗?
发布于 2020-11-07 21:04:25
您的代码中有很多事情发生,您没有为appStoreWrapper发布代码,但是这里有一些代码应该能够为您指明正确的方向。
FYI:
的文本
struct FilteredListItem: View { @State : Bool = false私有var alertDialog: AlertDialog?变量主体:一些视图{ Button(action:{ verifyItem() },label:{ Text("ITEM NAME") .foregroundColor(.white) }) .accentColor(.primary) .alert(isPresented:$showAlert,内容:{ getAlert() }} verifyItem() { // verifyItem(){//函数在这里验证项目的成功=真//appStoreWrapper.verifyPurChase.如果success { //句柄success } Handle { alertDialog?.selectedType = .mustBePurchased showAlert.toggle() } func purchaseItem() { //函数在这里购买项目,var success = true //appStoreWrapper.西蒙.如果成功( //句柄成功),则{ alertDialog?.selectedType = .purchaseError showAlert.toggle() } func getAlert() -> Alert {->getAlert= alertDialog Error{返回警报(标题:文本(“错误获取警报对话框”))}切换dialog.selectedType {。案例.purchaseError:返回警报(标题:文本(“错误购买项目”),消息:零,dismissButton:.default(文本(“OK”)) .mustBePurchased:返回警报(标题:文本(“物品必须购买”),消息:零,primaryButton:.default(文本(“购买”),行动:{ purchaseItem() },secondaryButton:.cancel() case .none,.purchase:返回警报(标题:文本(“购买”) }
https://stackoverflow.com/questions/64732089
复制相似问题