gpt4 book ai didi

swift - 在 Swift 中与 UIActivityViewController 共享字典

转载 作者:行者123 更新时间:2023-11-28 07:47:59 25 4
gpt4 key购买 nike

我是 Swift 的新手。我正在制作购物 list 应用程序。有两个UITextField,一个是元素名称,一个是保存到字典的元素数量。我想与 UIActivityViewController 共享此列表,但它只共享最后一个值。有人可以建议我如何与 UIActivityViewController 共享所有项目和金额。

var shoppinglist = [String:String]()

@IBOutlet weak var Item: UITextField!

@IBOutlet weak var Amount: UITextField!

@IBAction func Add(_ sender: UIButton) {
var item = Item.text!
var amount = Amount.text!
shoppinglist [item] = amount
print(shoppinglist)
}

@IBAction func share(_ sender: UIButton) {

let activityController = UIActivityViewController(activityItems: [shoppinglist], applicationActivities: nil)
present(activityController,animated: true,completion: nil)
}
}

最佳答案

试试下面的代码。我希望这个编码对你有所帮助。

class shoppinglist:NSObject {
var yourItem:String = ""
var amount:Int = 0
init(yourItem:String, amount:Int) {
self.yourItem = yourItem
self.amount = amount
}
}

在类中声明变量

var shoppingListArray = [shoppinglist]()
@IBOutlet weak var Item: UITextField!
@IBOutlet weak var Amount: UITextField!

@IBAction func Add(_ sender: UIButton) {
self.shoppingListArray.append(shoppinglist(yourItem:Item.text, amount:Int(Amount.text!)!))
print(shoppingListArray)
}

@IBAction func share(_ sender: UIButton) {
let activityController = UIActivityViewController(activityItems: shoppingListArray, applicationActivities: nil)
present(activityController,animated: true,completion: nil)
}
}

关于swift - 在 Swift 中与 UIActivityViewController 共享字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50481084/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com