tomiQrsd tomiQrsd - 4 months ago 17x
iOS Question

IBAction not getting called from Subviews button click

So I spent my whole evening trying to solve my issue that is clicking Button located in UIViewController will not trigger IBAction.

My situation

I'm building my app using storyboard and my main ViewController is a TabBarViewController. One of the items of said TabBarViewController is, let's call it

there is
which contains only camera preview and nothing else.

was added inside
as follows:

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
BackgrondViewController *bckgController = (BackgrondViewController *)[storyboard instantiateViewControllerWithIdentifier:@"background"];

[self.view addSubview:bckgController.view];
[self.view sendSubviewToBack:bckgController.view];

Everything works so far and I'm mentioning it becouse that might be relevant.


Now I want to add another ViewController to the
, this time on top. Let's call top view controller as

So first I instatiate it like this:

TopViewController *topController = (TopViewController *)[storyboard instantiateViewControllerWithIdentifier:@"top"];
topController.view.backgroundColor = [UIColor clearColor];

I added clearColor because there is need for that in my app and its confirmed that this changes nothing (trust me, I tried).

Okay now I add freshly created view as follows (we are still inside

[self.view addSubview:topController.view];
[self.view bringSubviewToFront:topController.view];

And it in fact does display transparent view with just a button in the center.

The Issue

No matter what I try I cannot make the button call its IBAction method.

Known facts and what I tried

  • TopViewController
    is actually on top of
    views, I checked that by getting all subviews of
    , taking the last element and comparing it to
    's view.

  • IBAction is correctly linked to the Button I need to click (confirmed via storyboard and mousing-over methods outlet)

  • Adding
    to TabBar works just fine, the Button does its job

  • The Button is visually clickable and it performs default animation when clicked

  • Setting
    topController.view.userInteractionEnabled = YES
    doesn't work

  • Adding single tap handler on
    and clicking anywhere on screen doesn't work, however adding it to
    does work (clicking anywhere on screen triggers defined action despite
    being on top)

  • All sizes, id's and corresponding classes are verified

  • viewDidLoad
    is getting called

Please help me get out of this jam. Thanks!


Are you retaining topController by using an instance variable or property?

A simple way to do this is to create a property in your MainViewController.h file.

@interface MainViewController : UIViewController
    @property (nonatomic, strong) TopViewController *topViewController;

Or at the top of your MainViewController.m file

@interface MainViewController ()
    @property (nonatomic, strong) TopViewController *topViewController;

Then set the property any time after you created it.

self.topViewController = topController;


-[UIView addSubview:] retains the view, but not the view controller. If you do not retain the view controller, then ARC will release the view controller at the end of the function. You can verify this by overriding -[TopViewController dealloc]. When you press the button, it tries to send a message to a now nil view controller.