ios – 以编程方式从UIBarButtonItem中截取

前端之家收集整理的这篇文章主要介绍了ios – 以编程方式从UIBarButtonItem中截取前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有三个视图控制器,在导航栏的右侧和左侧各有两个按钮,如下图所示.

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

  1. // Note: I am using FontAwesome as a third-party library.
  2.  
  3. class Helper: NSObject {
  4.  
  5. static func loadNavBarItems(vc: UIViewController) {
  6. let profileButton = UIBarButtonItem()
  7. let addButton = UIBarButtonItem()
  8.  
  9. let attributes = [NSFontAttributeName: UIFont.fontAwesome(ofSize: 20)] as [String: Any]
  10.  
  11. profileButton.setTitleTextAttributes(attributes,for: .normal)
  12. addButton.setTitleTextAttributes(attributes,for: .normal)
  13.  
  14. profileButton.title = String.fontAwesomeIcon(name: .userCircle)
  15. addButton.title = String.fontAwesomeIcon(name: .plus)
  16.  
  17. vc.navigationItem.leftBarButtonItem = profileButton
  18. vc.navigationItem.rightBarButtonItem = addButton
  19. }
  20.  
  21. func segueToProfile(vc: UIViewController) { // I need help here. }
  22.  
  23. }

然后我从每个VC的viewDidLoad()调用Helper.loadNavBarItems(vc:self).

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

问题是,我还没有完全理解如何通过选择器传递不同类型的参数,我可能在谷歌搜索中表现不好但是我找不到任何足够接近我的问题来理解如何实现这一点.

非常感谢您的帮助.

供参考,this is the structure of my Storyboard.

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

解决方法

我不知道我是否理解你的问题,但是为了使用segue在viewControllers(在你的情况下嵌入在UINavigationController中)之间传递数据,你可以在:
  1. override func prepare(for segue: UIStoryboardSegue,sender: Any?){
  2. if(segue.identifier == "yourIdentifier"){
  3. let navPreview : UINavigationController = segue.destination as! UINavigationController
  4. let preview : YourViewController = navPreview.viewControllers[0] as! YourViewController
  5. }}

猜你在找的iOS相关文章