如何在用户点击右侧栏按钮项目时显示弹出窗口?

How to present popUp when user taps on right bar button item?

我是 iOS 的新手。我在这个问题上挣扎了一段时间。我不知道如何设置当用户点击右栏按钮项时显示弹出窗口的逻辑。基本上,它应该是这样的:PopUp 我搜索了 Google,但我没有任何运气。如果有人能帮助我编写一些代码,我将不胜感激。

//我的VC

     override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        let rightBarButtonItem = UIBarButtonItem(title: "Share", style: .plain, target: self, action: #selector(clickShare))
        rightBarButtonItem.tintColor = UIColor.black
        navigationItem.rightBarButtonItem = rightBarButtonItem
   //     navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Share", style: UIBarButtonItemStyle.plain, target: self, action: #selector(clickShare))
        navigationItem.leftBarButtonItem = UIBarButtonItem(image: UIImage(named: "backArrow"), style: UIBarButtonItemStyle.plain, target: self, action: #selector(goBack))
        UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedStringKey.font : UIFont(name: "OpenSans", size: 14)! ], for: .normal)

    }

    //MARK: - Actions
   @objc func goBack() {
        navigationController?.popViewController(animated: true)
    }


    @objc func clickShare() {

    //this is where the logic should go
    }

//这是我的故事板: Storyboard

添加以下操作,

@IBAction func yourButtonClickAction(sender: UIBarButtonItem) {
    let storyboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
    let vc = storyboard.instantiateViewControllerWithIdentifier("YourViewController") as! UIViewController
    vc.modalPresentationStyle = UIModalPresentationStyle.Popover
    let popover: UIPopoverPresentationController = vc.popoverPresentationController!
    popover.barButtonItem = sender
    popover.delegate = self
    presentViewController(vc, animated: true, completion:nil)
}

将此操作添加到您的 barButtonItem。
'YourViewController' = 控制器由 Public、追随者选项组成。