我有三个视图控制器,在导航栏的右侧和左侧各有两个按钮,如下图所示.
我正在以编程方式创建这些按钮,而不是在每个相应的视图控制器(VC)中编写代码,我决定编写一个创建按钮的Helper类.
- // 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. }
- }
然后我从每个VC的viewDidLoad()调用Helper.loadNavBarItems(vc:self).
我现在要做的是在按下其中一个按钮时触发一个segue(让我们假设它是个人资料按钮).所以,我需要定义profile.action.因此,在Helper类中,我必须编写一个函数segueToProfile,它接受视图控制器(vc)并运行performSegueWithIdentifier.
问题是,我还没有完全理解如何通过选择器传递不同类型的参数,我可能在谷歌搜索中表现不好但是我找不到任何足够接近我的问题来理解如何实现这一点.
非常感谢您的帮助.
供参考,this is the structure of my Storyboard.
编辑:如故事板结构截图所示,我已经从三个视图控制器中的每一个创建了一个segue到目标视图控制器.
解决方法
我不知道我是否理解你的问题,但是为了使用segue在viewControllers(在你的情况下嵌入在UINavigationController中)之间传递数据,你可以在:
- override func prepare(for segue: UIStoryboardSegue,sender: Any?){
- if(segue.identifier == "yourIdentifier"){
- let navPreview : UINavigationController = segue.destination as! UINavigationController
- let preview : YourViewController = navPreview.viewControllers[0] as! YourViewController
- }}