首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SwiftUI -不清楚如何使用异步方法处理警报

SwiftUI -不清楚如何使用异步方法处理警报
EN

Stack Overflow用户
提问于 2020-11-07 20:24:20
回答 1查看 524关注 0票数 1

如果用户点击它,我必须在视图上显示一个警报。

我的警觉取决于几种情况:

购买

  1. 项目。显示该项目。
  2. 项目尚未购买。显示警告,告知用户必须购买的物品。此警报必须显示两个按钮,确定购买,取消关闭。
  3. 用户点击购买项目。
  4. 购买成功,显示项目。
  5. 购买失败,显示错误。

我就是这么做的。

代码语言:javascript
复制
  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(),对吗?但是我有这些异步方法

代码语言:javascript
复制
appStoreWrapper.verifyPurchase(productID: item.package!)
        { // run if purchased }, 
        runIfNotPurchased: { }

它不能将任何内容返回给警报修饰符。我该怎么做?我做的对吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-11-07 21:04:25

您的代码中有很多事情发生,您没有为appStoreWrapper发布代码,但是这里有一些代码应该能够为您指明正确的方向。

FYI:

  • 您可以使用带有操作的按钮,而不是使用.onTapGesture

的文本

  • .Alert中的代码应该只起作用以获得警报。您不应该在.Alert闭包中执行其他操作。

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:返回警报(标题:文本(“购买”) }

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64732089

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档