在 TableViewCell 中执行 UIDocumentPickerView
Perform UIDocumentPickerView in a TableViewCell
我正在尝试打开选择器文件菜单。
为此,我创建了一个 class,如下所示:
class pickerView: UIDocumentPickerViewController, UIDocumentPickerDelegate, LTHM_Pickerable {
func importTapped() {
//Create a picker specifying file type and mode
let documentPicker = UIDocumentPickerViewController(documentTypes: [String(kUTTypePNG)], in: .import)
documentPicker.delegate = self
documentPicker.allowsMultipleSelection = false
documentPicker.modalPresentationStyle = .fullScreen
present(documentPicker, animated: true, completion: nil)
}
public func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
guard controller.documentPickerMode == .import, let url = urls.first, let image = UIImage(contentsOfFile: url.path) else { return }
controller.dismiss(animated: true)
}
public func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) {
controller.dismiss(animated: true)
}
}
我想在 class 继承自 UITableViewCell 的函数中调用函数“importTapped”。
class LTHM_Sticker_Cell: UITableViewCell {
@IBAction func createStickerOnTAp(_ sender: UIButton, forEvent event: UIEvent) {
//CALL IMPORTTAPPED HERE
}
}
你能帮帮我吗?我试过一个协议,但我不确定...
创建常规 UIDocumentPickerViewController
实例的 UIDocumentPickerViewController
subclass 有点令人困惑,并且缺少实际呈现它的明确方法。您可能想试试这个 protocol/delegate 解决方案:
定义协议
protocol PickerPresenter: UIViewController {
func presentPicker()
}
创建将包含您的 table 视图的视图控制器 class
class MyViewController: UIViewController {
// your implementation
}
使您的视图控制器符合协议
extension MyViewController: PickerPresenter {
func presentPicker() {
let documentPicker = UIDocumentPickerViewController(documentTypes: [String(kUTTypePNG)], in: .import)
documentPicker.delegate = self
documentPicker.allowsMultipleSelection = false
documentPicker.modalPresentationStyle = .fullScreen
present(documentPicker, animated: true, completion: nil)
}
}
并成为选择代表
extension MyViewController: UIDocumentPickerDelegate {
public func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
guard controller.documentPickerMode == .import, let url = urls.first, let image = UIImage(contentsOfFile: url.path) else { return }
// do something with the selected image
controller.dismiss(animated: true)
}
public func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) {
controller.dismiss(animated: true)
}
}
在单元格出队时传递对视图控制器的引用
extension MyViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "myCell") as! LTHM_Sticker_Cell
cell.delegate = self
return cell
}
}
最后,您的单元格 class 具有对委托的弱引用,并在点击按钮时调用它的委托方法
class LTHM_Sticker_Cell: UITableViewCell {
weak var delegate: PickerPresenter?
@IBAction func createStickerOnTAp(_ sender: UIButton, forEvent event: UIEvent) {
delegate?.presentPicker()
}
}
我正在尝试打开选择器文件菜单。 为此,我创建了一个 class,如下所示:
class pickerView: UIDocumentPickerViewController, UIDocumentPickerDelegate, LTHM_Pickerable {
func importTapped() {
//Create a picker specifying file type and mode
let documentPicker = UIDocumentPickerViewController(documentTypes: [String(kUTTypePNG)], in: .import)
documentPicker.delegate = self
documentPicker.allowsMultipleSelection = false
documentPicker.modalPresentationStyle = .fullScreen
present(documentPicker, animated: true, completion: nil)
}
public func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
guard controller.documentPickerMode == .import, let url = urls.first, let image = UIImage(contentsOfFile: url.path) else { return }
controller.dismiss(animated: true)
}
public func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) {
controller.dismiss(animated: true)
}
}
我想在 class 继承自 UITableViewCell 的函数中调用函数“importTapped”。
class LTHM_Sticker_Cell: UITableViewCell {
@IBAction func createStickerOnTAp(_ sender: UIButton, forEvent event: UIEvent) {
//CALL IMPORTTAPPED HERE
}
}
你能帮帮我吗?我试过一个协议,但我不确定...
创建常规 UIDocumentPickerViewController
实例的 UIDocumentPickerViewController
subclass 有点令人困惑,并且缺少实际呈现它的明确方法。您可能想试试这个 protocol/delegate 解决方案:
定义协议
protocol PickerPresenter: UIViewController {
func presentPicker()
}
创建将包含您的 table 视图的视图控制器 class
class MyViewController: UIViewController {
// your implementation
}
使您的视图控制器符合协议
extension MyViewController: PickerPresenter {
func presentPicker() {
let documentPicker = UIDocumentPickerViewController(documentTypes: [String(kUTTypePNG)], in: .import)
documentPicker.delegate = self
documentPicker.allowsMultipleSelection = false
documentPicker.modalPresentationStyle = .fullScreen
present(documentPicker, animated: true, completion: nil)
}
}
并成为选择代表
extension MyViewController: UIDocumentPickerDelegate {
public func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
guard controller.documentPickerMode == .import, let url = urls.first, let image = UIImage(contentsOfFile: url.path) else { return }
// do something with the selected image
controller.dismiss(animated: true)
}
public func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) {
controller.dismiss(animated: true)
}
}
在单元格出队时传递对视图控制器的引用
extension MyViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "myCell") as! LTHM_Sticker_Cell
cell.delegate = self
return cell
}
}
最后,您的单元格 class 具有对委托的弱引用,并在点击按钮时调用它的委托方法
class LTHM_Sticker_Cell: UITableViewCell {
weak var delegate: PickerPresenter?
@IBAction func createStickerOnTAp(_ sender: UIButton, forEvent event: UIEvent) {
delegate?.presentPicker()
}
}