0

I am making sample project to implement top menu.

Below is the project structure.

enter image description here

TopMenu is the parent view with structure shown below.

enter image description here

In TopViewController, viewDidLoad below is what I have.

NSString *whereToGo = [[NSUserDefaults standardUserDefaults] valueForKey:@"whereToGo"]; // this is DarkGray by default
UIViewController *goToViewController;
goToViewController = [self.storyboard instantiateViewControllerWithIdentifier:whereToGo];
goToViewController.view.frame = bottomView.bounds;
// bottomView is the first view (will bring child here)
[bottomView addSubview:goToViewController.view];
[self addChildViewController:goToViewController];
[goToViewController didMoveToParentViewController:self];

Now when I execute project, I see Dark Gray, but when I click on Click Me button, nothing occurs (IBAction is not getting invoked).

- (IBAction)clickme:(id)sender {
    NSLog(@"clickme==dark gray");
}

What I was expecting is when I click Click Me, I will see clickme==dark gray in log.

Any reason why this is happening?

Sample project for download


Answer

As stated by MidhunMP, short answer is

menuView was hiding bottomView

Actually, for menuView I had backgroundColor as clearColor. And I assumed (which was wrong), as menuView is clear, touches won't work on menuView and those touches will go to bottomView.

Fahim Parkar
  • 30,974
  • 45
  • 160
  • 276

2 Answers2

1

Issue is simple, you are adding the goToViewController.view to bottomView.

The menuView is on top of bottomView, so it hides the touches to the goToViewController.view. You need to add the goToViewController.view to the topmost view, aka menuView.

Now your view hierarchy is like:

bottomView -> goToViewController.view -> menuView

You need to change that to:

bottomView -> menuView -> goToViewController.view

Change you code to following and check:

NSString *whereToGo = [[NSUserDefaults standardUserDefaults] valueForKey:@"whereToGo"]; // this is DarkGray by default
UIViewController *goToViewController;
goToViewController = [self.storyboard instantiateViewControllerWithIdentifier:whereToGo];
goToViewController.view.frame = menuView.bounds;
[menuView addSubview:goToViewController.view];
[self addChildViewController:goToViewController];
[goToViewController didMoveToParentViewController:self];
Midhun MP
  • 103,496
  • 31
  • 153
  • 200
  • All is well now, the issue was frame for my menu view was incorrect.. when I change to `menuView.frame = CGRectMake(0, 0, 320, 150);` all works great... – Fahim Parkar Jan 19 '15 at 07:06
  • @FahimParkar: If you need to load view controllers to the first ViewController. It's better to use ContainerView, a suggestion. :) – Midhun MP Jan 19 '15 at 07:07
  • do you mean in TopMenu? Can you elaborate more what I would need to do? – Fahim Parkar Jan 19 '15 at 07:09
  • @FahimParkar: Can you check [1](http://stackoverflow.com/questions/16884879/how-to-use-a-container-view-in-ios),[2](https://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/CreatingCustomContainerViewControllers/CreatingCustomContainerViewControllers.html),[3](http://mobileoop.com/how-to-use-container-view-controller) – Midhun MP Jan 19 '15 at 07:13
1

The view you are using as a container is covered by other views.

Andrea
  • 26,120
  • 10
  • 85
  • 131