我有三个视图控制器,每个控制器的导航栏左右两侧都有两个按钮,如下图所示。
我以编程方式创建了这些按钮,为了避免在每个视图控制器中重复编写代码,我决定编写一个帮助类来创建这些按钮。
我会在每个 ViewController 的 `viewDidLoad()` 中调用 `Helper.loadNavBarItems(vc: self)`。
现在我想要的是,在按下其中一个按钮时(假设是“Profile”按钮),触发一个 segue。所以,我需要定义 `profile.action`。因此,在 Helper 类中,我需要编写一个名为 `segueToProfile` 的函数,该函数接受一个视图控制器参数(`vc`),并运行 `performSegueWithIdentifier`。
问题在于,我不完全理解如何通过选择器传递不同类型的参数,而且我可能不擅长搜索,因为我找不到与我的问题足够接近的任何问题,以便我理解如何实现这一点。
非常感谢您的帮助。
供参考,这是我的故事板结构。
编辑:如故事版结构截图所示,我已经从每个 ViewController 创建了一个 segue 到目标 ViewController。
// 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. }
}
我会在每个 ViewController 的 `viewDidLoad()` 中调用 `Helper.loadNavBarItems(vc: self)`。
现在我想要的是,在按下其中一个按钮时(假设是“Profile”按钮),触发一个 segue。所以,我需要定义 `profile.action`。因此,在 Helper 类中,我需要编写一个名为 `segueToProfile` 的函数,该函数接受一个视图控制器参数(`vc`),并运行 `performSegueWithIdentifier`。
问题在于,我不完全理解如何通过选择器传递不同类型的参数,而且我可能不擅长搜索,因为我找不到与我的问题足够接近的任何问题,以便我理解如何实现这一点。
非常感谢您的帮助。
供参考,这是我的故事板结构。
编辑:如故事版结构截图所示,我已经从每个 ViewController 创建了一个 segue 到目标 ViewController。
loadNavBarItems
函数中。 - haik.ampardjianprofileButton.action
时传递参数给选择器。 - Fares K. A.UIViewController
)上,这些视图控制器嵌套在导航控制器中。 - Fares K. A.