Swift:使用未解析的标识符“ present”

时间:2019-05-16 02:48:40

标签: ios swift

我在UIView上添加了UIViewController,其中有一些代码

ViewController.swift

class ViewController: UIViewController {
   override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
    let listView = ListView()
    self.view.addSubview(listView)
 }

ListView.swift

 class ListView: UIView {
   override init(frame: CGRect) {
    super.init(frame: frame)
    self.addSubview(btnGO)
   }

   convenience init() {
    self.init(frame: CGRect.zero)
   }

   required init(coder aDecoder: NSCoder) {
    fatalError("This class does not support NSCoding")
   }

   lazy var btnGO:(UIButton) = {
    let btn = UIButton(frame: CGRect(x: 0, y: 0, width: 44, height: 34))
    btn.addTarget(self, action: #selector(btnClicked), for: .touchUpInside)
    //some codes
    return btn
  }()

  @objc func btnClicked(sender:UIButton) {
    let shareWOW = "some content"
    let objectsToShare = [shareWOW]
    let activityController = UIActivityViewController(
        activityItems: objectsToShare,
        applicationActivities: nil)
    activityController.popoverPresentationController?.sourceRect = self.frame
    activityController.popoverPresentationController?.sourceView = self
    activityController.popoverPresentationController?.permittedArrowDirections = .any
    present(activityController, animated: true, completion: nil)
  }
 }

我总是得到错误信息Use of unresolved identifier 'present',我知道UIActivityViewController应该放在UIViewController中,我的问题是我将函数btnClicked放在ViewController.swift中,如何调用它在UIView中用作ListView.swift?非常感谢

1 个答案:

答案 0 :(得分:0)

present(_:animated:completion:)方法应与UIViewController实例一起使用。您不能在UIView子类中调用此方法。使用委托从ViewController中调用ListView中的方法,并像这样从UIActivityViewController中显示ViewController

ViewController.swift

class ViewController: UIViewController, ListViewDelegate {
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        let listView = ListView()
        listView.delegate = self
        self.view.addSubview(listView)
    }
    func btnClicked(sender: UIButton) {
        let shareWOW = "some content"
        let objectsToShare = [shareWOW]
        let activityController = UIActivityViewController(
            activityItems: objectsToShare,
            applicationActivities: nil)
        activityController.popoverPresentationController?.sourceRect = sender.frame
        activityController.popoverPresentationController?.sourceView = sender
        activityController.popoverPresentationController?.permittedArrowDirections = .any
        self.present(activityController, animated: true, completion: nil)
    }
}

ListView.swift

protocol ListViewDelegate {
    func btnClicked(sender:UIButton)
}

class ListView: UIView {

    var delegate: ListViewDelegate?

    override init(frame: CGRect) {
        super.init(frame: frame)
        self.addSubview(btnGO)
    }

    convenience init() {
        self.init(frame: CGRect.zero)
    }

    required init(coder aDecoder: NSCoder) {
        fatalError("This class does not support NSCoding")
    }

    lazy var btnGO:(UIButton) = {
        let btn = UIButton(frame: CGRect(x: 0, y: 0, width: 44, height: 34))
        btn.addTarget(self, action: #selector(btnClicked), for: .touchUpInside)
        //some codes
        return btn
    }()

    @objc func btnClicked(sender:UIButton) {
        delegate?.btnClicked(sender: sender)
    }
}