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

时间:2018-07-09 12:49:56

标签: ios popupwindow uistoryboardsegue

我在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

1 个答案:

答案 0 :(得分:1)

添加以下操作,

@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” =控制器,由“公共”,“关注者”选项组成。