用户点击右栏按钮项

用户点击右栏按钮项

我在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,关注者选项组成。

关于ios - 用户点击右栏按钮项时如何显示弹出窗口?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51246007/

10-09 16:23