0

I have a situation where I have a custom UIButton that is a subview of a UIBarButtonItem:

guard let tab1 = self.tabBar.items![0].value(forKey: "view") as? UIView else {return}

    let button2Test = UIButton()
    tab1.addSubview(button2Test)

The custom UIButton, has a target:

button2Test.addTarget(self, action: #selector(buttonTouchUpInsideP3), for: [.touchUpInside])
button2Test.addTarget(self, action: #selector(buttonTouchInsideBoundsP3), for: [.touchDown, .touchDragEnter])
button2Test.addTarget(self, action: #selector(buttonDraggedOutOfBoundsP3), for: [.touchDragExit, .touchCancel])

Basically, in the buttonTouchUpInsideP3 function, I want to programmatically / manually make the UIBarButtonItem at a specific index to fire as if it was tapped on, since the target of the UIButton does not allow the actual UIBarButtonItem underneath to be tapped, unfortunately...

My attempt which did not work because of errors is: (which I got from an outdated Q). Note that I call this in the buttonTouchUpInsideP3 target function which ones once you've let go of the button.

let homeTabBarItem = self.tabBar.items![0]
UIApplication.sharedApplication.sendAction(homeTabBarItem.action, to: homeTabBarItem.target, from: nil, forEvent: nil)
  • @Frankenstein well, I added more explanation and appropriate code samples. –  Jun 14 '20 at 06:59
  • Where are the class and functions? – Frankenstein Jun 14 '20 at 07:02
  • @Frankenstein Well this SO question is about how to do a specific thing, not a bug fix issue sort of thing... it is not an issue you have to replicate, just a question on how to do something as I cannot seem to find a question on it... –  Jun 14 '20 at 07:20

2 Answers2

0

You can just set the UIBarButtonItem's target and action properties directly.

ERP
  • 325
  • 2
  • 11
  • well the thing is that I set the UITabBar and the tabs in the Storyboard... I would like to use whatever target and action that the native one that's set because of the Storyboard stuff, and simply call it manually. So in practice basically, I want to act as if I selected the bar button item, when the TouchUpInisde target fund runs for my custom UIButton... –  Jun 14 '20 at 06:07
  • What is the purpose of having a UIButton as a subview of a UIBarButtonItem? I am not sure I understand. A UIBarButtonItem can already act as a Button, so I don't see the need. – ERP Jun 14 '20 at 06:15
  • I use the UIButton so that I can do custom animation for when you are doing a touchDown or a touchExit/touchCancel etc. on the UIButton which also serves as my icon on my tab bar. –  Jun 14 '20 at 06:17
  • Ok I see, so maybe there is where you have the problem, have you checked if the UIBarButtonItem is triggered before the UIButton? If that happens the action will never reach UIButton because UIBarButtonItem is the first respondent in this case and the selector will be never triggered – ERP Jun 14 '20 at 06:20
  • Well the issue is that since my custom UIButton is above the UIBarButtonItem, the UIBarButtonItem is simply never triggered, that's why I want to instead trigger it manually. My custom UIButton essentially serves as the UIBarButtonItem, all I need is to be able to trigger the UIBarButtonItem (that, is underneath..) programmatically... –  Jun 14 '20 at 06:25
  • Thats easy, you just make UIButton.isUserInteractionEnabled = false and UIBarButtonItem should be triggered in that case – ERP Jun 14 '20 at 06:26
  • Well thanks for the attempt, unfortunately, that does not seem to work in my situation :/ –  Jun 14 '20 at 06:34
  • UIBarButtonItem is still not being triggered? It should in that case – ERP Jun 14 '20 at 06:40
0

I figured out how to do it:

tabBarController.selectedIndex = 0

Just put this in the buttonTouchUpInside function! ;)

You should probably also call:

self.tabBar(self.tabBar, didSelect: self.tabBar.items![0])