首页 > 解决方案 > 如何添加警报按钮直到我的数据计数?如何保存从操作索引中选择的数据?

问题描述

当我单击按钮时,我会显示警报。我从列表中选择(如果有多少数据可用)。如何从列表索引中选择保存我的数据?

你可以在这里看到 UI

我的账户服务

class AccountServices {
    static let databaseReference = Database.database().reference(withPath: "Accounts").child((Auth.auth().currentUser?.uid)!)

    static var account = Account()

    static func saveChanges() {
        databaseReference.setValue(try! FirebaseEncoder().encode(AccountServices.account))
    }

    static func getAccount() {
        databaseReference.observeSingleEvent(of: .value, andPreviousSiblingKeyWith: { (snapshot, _) in
            account = try! FirebaseDecoder().decode(Account.self, from: snapshot.value!)
        })
    } 
}

多变的

var product: ProductViewModel?

addButton 点击

@IBAction func addToCartButtonTapped(_ sender: UIButton) {
    let alert = UIAlertController(title: "Bu ürünü hangi sepetinize eklemek istersiniz ?", message: "", preferredStyle: .actionSheet)
    var indexer = 0
    for cart in AccountServices.account.cart! {
        if cart.product == nil{
            AccountServices.account.cart![indexer].product = [Product]()
        }
        let action = UIAlertAction(title: cart.name , style: .default, handler: { (sender) in


            if let index = alert.actions.firstIndex(where: { $0 === sender }) {
                AccountServices.account.cart?[index].product?.append(self.product) `//Error: Cannot convert value of type 'ProductViewModel?' to expected argument type 'Product'`
                    AccountServices.saveChanges()//TODO...

                }
                let addAlert = UIAlertController(title: "Sepetinize Eklendi.", message: "Ürününüz sepetinize eklendi.", preferredStyle: .alert)
                let okButton = UIAlertAction(title: "Tamam", style: .default, handler: nil)
                addAlert.addAction(okButton)
                self.present(addAlert, animated: true, completion: nil)
            })
            alert.addAction(action)
            indexer += 1
        }

        let cancelaction = UIAlertAction(title: "Vazgeç", style: .cancel, handler: nil)
        alert.addAction(cancelaction)
        present(alert, animated: true, completion: nil)
    }
}

标签: swiftxcodeuialertcontroller

解决方案


推荐阅读