以编程方式从UIBarButtonItem中截取 [英] Programmatically segue from UIBarButtonItem

查看:108
本文介绍了以编程方式从UIBarButtonItem中截取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个视图控制器,在导航栏的左右两侧各有两个按钮,如下图所示。

I have three view controllers, all with two buttons each on the right and left sides of the navigation bar, as seen below on one of them.

我正在以编程方式创建这些按钮,而不是在每个相应的视图控制器(VC)中编写代码,我决定编写一个创建按钮的助手类。

I'm creating these buttons programatically, and instead of writing the code in each respective view controller (VC) I decided to write a Helper class that creates the buttons.

// Note: I am using FontAwesome as a third-party library.

class Helper: NSObject {

    static func loadNavBarItems(vc: UIViewController) {
        let profileButton = UIBarButtonItem()
        let addButton = UIBarButtonItem()

        let attributes = [NSFontAttributeName: UIFont.fontAwesome(ofSize: 20)] as [String: Any]

        profileButton.setTitleTextAttributes(attributes, for: .normal)
        addButton.setTitleTextAttributes(attributes, for: .normal)

        profileButton.title = String.fontAwesomeIcon(name: .userCircle)
        addButton.title = String.fontAwesomeIcon(name: .plus)

        vc.navigationItem.leftBarButtonItem = profileButton
        vc.navigationItem.rightBarButtonItem = addButton
    }

    func segueToProfile(vc: UIViewController) { // I need help here. }

}

然后我调用 Helper。 loadNavBarItems(vc:self)来自每个VC的 viewDidLoad()

I then call Helper.loadNavBarItems(vc: self) from each VC's viewDidLoad().

我是什么我现在要做的就是在按下其中一个按钮时触发一个segue(让我们假设它是个人资料按钮)。所以,我需要定义 profile.action 。所以,在Helper类中,我必须编写一个函数 segueToProfile ,它接受一个视图控制器( vc )并运行 performSegueWithIdentifier

What I'm trying to do now is to trigger a segue when one of the buttons is pressed (let's assume it's the profile button). So, I need to define profile.action. So, in the Helper class, I have to write a function segueToProfile that takes a view contoller (vc) and runs performSegueWithIdentifier.

问题是,我不完全了解如何通过选择器传递不同类型的参数,我可能对谷歌不好,但我找不到任何足够接近我的问题,以便了解如何实现这一目标。

The problem is, I'm not fully understanding how to pass in different types of parameters through selectors, and I may be bad at Googling but I cannot find any questions that are close enough to mine for me to understand how to achieve this.

非常感谢您提前帮助。

作为参考,这是我的结构故事板

编辑:如故事板结构截图中所示,我已经从三个中创建了一个segue查看控制器到目标视图控制器。

As shown in the storyboard structure screenshot, I've already created a segue from each of the three view controllers to the destination view controller.

推荐答案

创建barButtonItem:

To create barButtonItem:

这篇关于以编程方式从UIBarButtonItem中截取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆