Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Container view using Storyboard, interaction and view issues

I've been trying to implement a View layout with two simple containers side by side in a iPad dashboard manner.

I've been reading through numerous examples and of course the Apple View Controller reference for view controllers and in general but i have a most strange issue which hopefully have a simple explanation.

I've added the two view containers side by side, each which will house a viewcontroller having a different background color, one Round Rect Button and a label. And for testing purposes i've also added a Round Rect button to the "mainViewController".

So fairly straight forward and simple. Each view gets loaded in the container view, however i can't interact with any of them (when button is clicked, NOTHING happens, no blue highlighting, so it's not a question of delegating or any action from the buttons is received or not because nothing like that is implemented). However clicking the button i've put in the "mainViewController" holding the two containers, works flawlessly (flashing blue as always). Another strange thing is that the background color is not shown on the container views either?! Only the buttons and labels.

Program as follows:

Files: ViewController.h and .m (is "mainViewController") sidebarViewController.h and .m (nothing changed in these files, UIViewController) resultsViewController.h and .m (nothing changed in these files, UIViewController) AppDelegate.h and .m (no change) Storyboard

ViewController.h, outlets created and connected to each container view in the Storyboard.

#import <UIKit/UIKit.h>
#import "sidebarViewController.h"
#import "resultsViewController.h"

@interface ViewController : UIViewController

@property (nonatomic, weak) IBOutlet UIView *sidebarView;
@property (nonatomic, weak) IBOutlet UIView *resultsView;


@end

Storyboard identifier set to both sidebarViewcontroller and Resultsviewcontroller in storyboard.

ViewController.m, everything done by the book (or probably not, but what at least it seems like to me)

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

@synthesize sidebarView,resultsView;

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

UIStoryboard *storyboard = self.storyboard;
sidebarViewController *sideVC  = [storyboard instantiateViewControllerWithIdentifier:@"SidebarViewController"];
resultsViewController *resultsVC  = [storyboard instantiateViewControllerWithIdentifier:@"ResultsViewController"];

//sidebarViewController *sideVC = [[sidebarViewController alloc]init];
//rresultsViewController *resultsVC = [[resultsViewController alloc]init];
[self addChildViewController:sideVC];
sideVC.view.frame = self.sidebarView.frame;
[self.sidebarView addSubview:sideVC.view];
self.sidebarView.userInteractionEnabled = YES;
[sideVC didMoveToParentViewController:self];

[self addChildViewController:resultsVC];
resultsVC.view.frame = self.resultsView.frame;
[self.resultsView addSubview:resultsVC.view];
self.resultsView.userInteractionEnabled = YES;
[resultsVC didMoveToParentViewController:self];

}

(i've had self.xxxxView as well, and also had the addChildViewController statement first in order)

Ok so my suspicion is of course that it has something to do with the view hierarchy, the views ar "behind" the rootViewController view, and therefor only shown not clickable. What (if something) needs to be done in the App delegate to get this to work? Or have i done something else wrong in this code?

Edit: Found out another clue to what might be wrong: When I disabled "Auto Layout" for the storyboard the views showed up and worked (!) but completely distorted, but now at least i get correct background color and the buttons are working. Any one know how to fix this without disabling auto layout? since i guess i have to do all my layout programatically then? :(

Many thanks,

like image 804
Andreas Klintberg Avatar asked Nov 18 '12 18:11

Andreas Klintberg


1 Answers

All that code you have is unnecessary, and is probably what is causing your problems. When you added your container views in IB, it automatically creates those embedded view controllers for you (and resizes them to be the size of the container view). Did you get those? All you need to do is change their classes to the classes of your subclassed view controllers. If you need to access these controllers from your "main" view controller, you can get a reference to them from the childViewControllers property.

like image 75
rdelmar Avatar answered Oct 31 '22 03:10

rdelmar