在 Swift 中与 UIActivityViewController 共享字典
Posted
技术标签:
【中文标题】在 Swift 中与 UIActivityViewController 共享字典【英文标题】:Sharing Dictionary with UIActivityViewController in Swift 【发布时间】:2018-05-23 06:18:03 【问题描述】:我是 Swift 新手。我正在制作购物清单应用程序。有两个 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)
【问题讨论】:
【参考方案1】:试试下面的代码。我希望这个编码对你有所帮助。
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)
【讨论】:
【参考方案2】:试试这个:-
@IBAction func share(_ sender: UIButton)
let activityVc = UIActivityViewController(activityItems: ["here your item name","item quantity"], applicationActivities:nil)
activityVc.popoverPresentationController?.sourceView = self.view
self.present(activityVc, animated:true, completion:nil)
【讨论】:
以上是关于在 Swift 中与 UIActivityViewController 共享字典的主要内容,如果未能解决你的问题,请参考以下文章