gpt4 book ai didi

ios - 如何从 UIActivityItemSource 函数返回多个值

转载 作者:行者123 更新时间:2023-11-28 07:34:26 30 4
gpt4 key购买 nike

是否可以从 UIActivityItemSource 函数返回多个值,并将它们传递给 UIActivityViewController 的 activityItems?

我想返回 [message, image, url] 数组并传递给 activityItems。但是我使用的切换代码只返回一个值,消息、图像或 url,这显然取决于选择的事件。

如果我改变:func activityViewController(_ activityViewController: UIActivityViewController, itemForActivityType activityType: UIActivity.ActivityType?) -> Any? 到:func.... -> [Any]?

UIActivityItemSource 提示:

"Type 'ActivityItemSource' does not conform to protocol 'UIActivityItemSource'"

然后让你用“-> Any?”插入另一组相同的函数

也许我不理解如何处理 init(),但如果有人能给我解决这个问题的方向,我将不胜感激。

我在 Multiple data from UIActivityItemSource 发现了一个类似的问题张贴者自己认为:“原来我需要做的就是向事件 Controller 提供多个项目,因为每个事件项目源只能提供一个项目。”

这是否意味着我需要为消息创建 ActivityItemSource1 类,为图像创建 ActivityItemSource2 类,为 url 创建 ActivityItemSource3 类?并像这样将它们分配给 activityItems?:

让 activityItems = [ActivityItemSource1(message: message), ActivityItemSource2(image: image), ActivityItemSource3(url: url)]

============================================= ===================

class ActivityItemSource: NSObject, UIActivityItemSource {

var message: String!
var image: UIImage!
var url: URL!

init(message: String, image: UIImage, url: URL) {
self.message = message
self.image = image
self.url = url
}

func activityViewControllerPlaceholderItem(_ activityViewController: UIActivityViewController) -> Any {
return message
}

func activityViewController(_ activityViewController: UIActivityViewController, itemForActivityType activityType: UIActivity.ActivityType?) -> Any? {

switch activityType {
case UIActivity.ActivityType.postToFacebook:
return image
case UIActivity.ActivityType.postToTwitter:
return message
case UIActivity.ActivityType.mail:
return message
case UIActivity.ActivityType.copyToPasteboard:
return message
case UIActivity.ActivityType.markupAsPDF:
return message
case UIActivity.ActivityType.message:
return message
case UIActivity.ActivityType.postToFlickr:
return image
case UIActivity.ActivityType.postToTencentWeibo:
return message
case UIActivity.ActivityType.postToVimeo:
return image
case UIActivity.ActivityType.print:
return message
case UIActivity.ActivityType(rawValue: "com.apple.reminders.RemindersEditorExtension"):
return message
case UIActivity.ActivityType(rawValue: "com.apple.mobilenotes.SharingExtension"):
return message

default:
return message

}

}
}


class TodaysTasksTableViewcontroller: TableViewController {
var image: UIImage
var message: String
var url: URL

image = UIImage(named: "PoliPoliIconLarge")!
message = "I will complete the following task(s) today :"
url = URL(string: "http://www.beckos.com")!

let activityItems = [ActivityItemSource(message: message, image: image, url: url)]

let activityVC = UIActivityViewController(activityItems: activityItems, applicationActivities: nil)

// Exclude some activities from share popup screen
activityVC.excludedActivityTypes = [
UIActivity.ActivityType.assignToContact,
UIActivity.ActivityType.print,
UIActivity.ActivityType.addToReadingList,
UIActivity.ActivityType.saveToCameraRoll,
UIActivity.ActivityType.openInIBooks,
//UIActivity.ActivityType(rawValue: "com.apple.reminders.RemindersEditorExtension"),
//UIActivity.ActivityType(rawValue: "com.apple.mobilenotes.SharingExtension"),
]


self.present(activityVC, animated: true, completion: nil)

................
}

最佳答案

这是我昨晚做的。不过,我不确定这种方式在正确的编码风格方面是否有效。此外,我不确定我是否真正了解 ActivitySourceItems 的工作方式。有人可能可以用更有效的方式编写它。

基本上我创建了三个类,一个用于消息,一个用于图像,一个用于 url。然后传递三个返回一个常量,activityItems。不过,Instagram 和 Snapchat 仍然不适用于此。除此之外,它还是做了我想做的事。

感谢 Larme 提供的链接。我之前的搜索没有显示这些帖子。

class TableViewController: UITableViewController {     
let activityItems = [ActivityItemSource(message: message), ActivityItemSourceImage(image: image), ActivityItemSourceURL(url: url)]
let activityVC = UIActivityViewController(activityItems: activityItems, applicationActivities: nil)

// some exclusion goes here...

self.present(activityVC, animated: true, completion: nil)

}

class ActivityItemSource: NSObject, UIActivityItemSource {

var message: String!

init(message: String) {
self.message = message
}

func activityViewControllerPlaceholderItem(_ activityViewController: UIActivityViewController) -> Any {
return message
// to display Instagram button, return image
// image: Mail, Message, Notes, Twitter, Instagram, Shared Album, Post to Google Maps, Messenger, LINE, Snapchat, Facebook
// message: Mail, Message, Notes, Twitter, Messenger, LINE, Facebook, LinkedIn
}

func activityViewController(_ activityViewController: UIActivityViewController, itemForActivityType activityType: UIActivity.ActivityType?) -> Any? {

switch activityType {
case UIActivity.ActivityType.postToFacebook:
return nil
case UIActivity.ActivityType.postToTwitter:
return message
case UIActivity.ActivityType.mail:
return message
case UIActivity.ActivityType.copyToPasteboard:
return message
case UIActivity.ActivityType.markupAsPDF:
return message
case UIActivity.ActivityType.message:
return message
case UIActivity.ActivityType.postToFlickr:
return message
case UIActivity.ActivityType.postToTencentWeibo:
return message
case UIActivity.ActivityType.postToVimeo:
return message
case UIActivity.ActivityType.print:
return message
case UIActivity.ActivityType(rawValue: "com.apple.reminders.RemindersEditorExtension"):
return message
case UIActivity.ActivityType(rawValue: "com.apple.mobilenotes.SharingExtension"):
return message
case UIActivity.ActivityType(rawValue: "com.burbn.instagram.shareextension"):
return nil
case UIActivity.ActivityType(rawValue: "jp.naver.line.Share"):
return message

default:
return nil
}
}
}


class ActivityItemSourceImage: NSObject, UIActivityItemSource {

var image: UIImage!


init(image: UIImage) {
//self.image = image
self.image = UIImage(named: "PoliPoliIcon")!

}


func activityViewControllerPlaceholderItem(_ activityViewController: UIActivityViewController) -> Any {
return image
}

func activityViewController(_ activityViewController: UIActivityViewController, itemForActivityType activityType: UIActivity.ActivityType?) -> Any? {


switch activityType {
case UIActivity.ActivityType.postToFacebook:
return nil
case UIActivity.ActivityType.postToTwitter:
return nil
case UIActivity.ActivityType.mail:
return image
case UIActivity.ActivityType.copyToPasteboard:
return image
case UIActivity.ActivityType.markupAsPDF:
return image
case UIActivity.ActivityType.message:
return image
case UIActivity.ActivityType.postToFlickr:
return image
case UIActivity.ActivityType.postToTencentWeibo:
return image
case UIActivity.ActivityType.postToVimeo:
return image
case UIActivity.ActivityType.print:
return image
case UIActivity.ActivityType(rawValue: "com.apple.reminders.RemindersEditorExtension"):
return nil
case UIActivity.ActivityType(rawValue: "com.apple.mobilenotes.SharingExtension"):
return nil
case UIActivity.ActivityType(rawValue: "com.burbn.instagram.shareextension"):
return image
case UIActivity.ActivityType(rawValue: "jp.naver.line.Share"):
return image
default:
return image

}
}

}


class ActivityItemSourceURL: NSObject, UIActivityItemSource {

var url: URL!


init(url: URL) {
self.url = url
}

func activityViewControllerPlaceholderItem(_ activityViewController: UIActivityViewController) -> Any {
return url

}

func activityViewController(_ activityViewController: UIActivityViewController, itemForActivityType activityType: UIActivity.ActivityType?) -> Any? {

switch activityType {
case UIActivity.ActivityType.postToFacebook:
return url
case UIActivity.ActivityType.postToTwitter:
return url
case UIActivity.ActivityType.mail:
return url
case UIActivity.ActivityType.copyToPasteboard:
return nil
case UIActivity.ActivityType.message:
return url
case UIActivity.ActivityType.postToFlickr:
return url
case UIActivity.ActivityType.postToTencentWeibo:
return url
case UIActivity.ActivityType.postToVimeo:
return url
case UIActivity.ActivityType.print:
return url
case UIActivity.ActivityType(rawValue: "com.apple.reminders.RemindersEditorExtension"):
return url
case UIActivity.ActivityType(rawValue: "com.apple.mobilenotes.SharingExtension"):
return url
case UIActivity.ActivityType(rawValue: "com.burbn.instagram.shareextension"):
return nil
case UIActivity.ActivityType(rawValue: "jp.naver.line.Share"):
return url
//case UIActivity.ActivityType(rawValue: "com.snapchat.Share"):
// return nil

default:
return url

}
}
}

关于ios - 如何从 UIActivityItemSource 函数返回多个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53605543/

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