swift - 如何添加警报按钮直到我的数据计数?如何保存从操作索引中选择的数据?
问题描述
当我单击按钮时,我会显示警报。我从列表中选择(如果有多少数据可用)。如何从列表索引中选择保存我的数据?
你可以在这里看到 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)
}
}
解决方案
推荐阅读
- python - 使用 Pandas 和 Curve_fit 推断错误 func() 需要 3 个位置参数,但给出了 4 个
- r - kableExtra linebreak 不能产生换行符
- c# - 实体框架 - 键 'PRIMARY' 的重复条目 '1'"
- excel - 我的 vba 只选择活动工作表中的表
- json - 使用ansible命令模块时如何从CURL请求的json响应中检索值?
- python - 使用 Python 将数据写入 FireBase
- html - HTML中的span和i标记有什么区别
- c# - UWP 地图控制自 2020 年 11 月 16 日起崩溃
- python - 将变量添加到文件路径变量中
- mysql - MySQL在没有更多子查询的情况下获得不同维度的聚合