views:

370

answers:

3

I'm trying to switch a currently loaded view with a new one.

I have 3 xib files and 3 sets of ViewControllerFiles.

RootViewController loads correctly, and correctly loads HomeViewController.

The line that errors currently is this

[self.rootViewController.view insertSubview:gameController.view atIndex:1];

error: request for member 'view' in something not a structure or a union

If anyone could point me in the right direction that would be great. I understand how I could switch views all from the root controller but I want to swap/add views from a subview. If that makes sense?

Here is the root controller header and implementation.

#import <UIKit/UIKit.h>
@class HomeViewController;
@class GameViewController;
@interface RootViewController : UIViewController {
HomeViewController *homeViewController;
GameViewController *gameViewController;
}
@property (retain, nonatomic) HomeViewController *homeViewController;
@property (retain, nonatomic) GameViewController *gameViewController;
- (IBAction)showHome:(id)sender;
- (IBAction)showGame:(id)sender;
@end

Implemenation file

#import "RootViewController.h"
#import "HomeViewController.h"
#import "GameViewController.h"
@implementation RootViewController
@synthesize homeViewController;
@synthesize gameViewController;

- (void)viewDidLoad {

homeViewController.rootViewController = self;

HomeViewController *homeController = [[HomeViewController alloc]    initWithNibName:@"HomeView" bundle:nil];
self.homeViewController = homeController; 
[self.view insertSubview:homeController.view atIndex:0]; 
[homeController release];   

[super viewDidLoad];
}

- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];    

if (self.homeViewController.view.superview == nil)
 self.homeViewController = nil;
else
 self.gameViewController = nil;
    }

- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
  • (void)dealloc { [homeViewController release]; [gameViewController release]; [super dealloc]; } @end

And here is the home view files which load correctly

#import <UIKit/UIKit.h>
@class RootViewController;

@interface HomeViewController : UIViewController {  
//GameViewController *gameViewController;
RootViewController *rootViewController;
}

//@property (retain, nonatomic) GameViewController *gameViewController;
@property (nonatomic, assign) RootViewController *rootViewController;

- (IBAction)showGame:(id)sender;
@end

Implementation file

#import "HomeViewController.h"
#import "RootViewController.h"
@implementation HomeViewController;
@synthesize rootViewController;
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {   
[super viewDidLoad];
}
- (IBAction)showGame:(id)sender{
GameViewController *gameController = [[GameViewController alloc] initWithNibName:@"GameView" bundle:nil];
self.rootViewController = gameController;
[self.rootViewController.view insertSubview:gameController.view atIndex:1];
[gameController release];   
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];    
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
}
@end
+1  A: 

I'm confused by your comment. Here is the additional code snippet:

- (IBAction)showGame:(id)sender { 
    GameViewController *gameController = [[GameViewController alloc] initWithNibName:@"GameView" bundle:nil]; 
    self.rootViewController = gameController; 
    [self.rootViewController.view insertSubview:gameController.view atIndex:1];         
    [gameController release]; 
}

So you're setting the rootViewController of the HomeViewController to equal gameController. Then you're inserting the gameController view into the rootViewController's view. But, the gameController is the rootViewController. I think you have a logic error here.

Justin Gallagher
A: 

I think this is your problem:

- (void)viewDidLoad {

homeViewController.rootViewController = self;

HomeViewController *homeController = [[HomeViewController alloc]  initWithNibName:@"HomeView" bundle:nil];
self.homeViewController = homeController;

You assign the rootViewController to homeViewController before you initialize homeViewController. At that point `self.homeViewController' is just empty memory.

TechZen
+1  A: 

This is luckily an easy fix - you forgot to include the GameViewController header file. Hence it has no idea what the view property of your GameViewController instance is.

bpapa