views:

40

answers:

1

I am stuck in a tutorial, trying to switch views with a toolbar. My code builds fine and I put some alerts in to check the toolbar was working, and it is. But my subviews I am inserting are just not showing up at all. Can anybody help please?

Here is my main controller code..

MainViewController.h is


#import 

@class FlavourPickerView;
@class ShakeView;
@class ResultsView;

@interface MainViewController : UIViewController {

    IBOutlet FlavourPickerView *flavourPickerView;
    IBOutlet ShakeView *shakeView;
    IBOutlet ResultsView *resultsView;
}

@property (retain,nonatomic) FlavourPickerView *flavourPickerView;
@property (retain,nonatomic) ShakeView *shakeView;
@property (retain,nonatomic) ResultsView *resultsView;

-(IBAction)loadFlavourPickerView:(id)sender;
-(IBAction)loadShakeView:(id)sender;
-(IBAction)loadResultsView:(id)sender;

-(void)clearView;

@end


and here is MainViewController.m

#import "MainViewController.h"
#import "FlavourPickerView.h"
#import "ShakeView.h"
#import "ResultsView.h"

@implementation MainViewController

@synthesize flavourPickerView;
@synthesize shakeView;
@synthesize resultsView;


-(IBAction)loadFlavourPickerView:(id)sender{
 UIAlertView *alertDialog;
 alertDialog = [[UIAlertView alloc]
       initWithTitle:@"Flavour selected"
       message:@"Seems to work"
       delegate:nil
       cancelButtonTitle:@"OK!"
       otherButtonTitles:nil];
 [alertDialog show];
 [alertDialog release];

 [self clearView];
 [self.view insertSubview:flavourPickerView.view atIndex:10];
}


-(IBAction)loadShakeView:(id)sender{
 UIAlertView *alertDialog;
 alertDialog = [[UIAlertView alloc]
       initWithTitle:@"Shaker selected"
       message:@"Seems to work"
       delegate:nil
       cancelButtonTitle:@"OK!"
       otherButtonTitles:nil];
 [alertDialog show];
 [alertDialog release];

 [self clearView];
 [self.view insertSubview:shakeView.view atIndex:0];
}


-(IBAction)loadResultsView:(id)sender{
 UIAlertView *alertDialog;
 alertDialog = [[UIAlertView alloc]
       initWithTitle:@"Results selected"
       message:@"Seems to work"
       delegate:nil
       cancelButtonTitle:@"OK!"
       otherButtonTitles:nil];
 [alertDialog show];
 [alertDialog release];

 [self clearView];
 [self.view insertSubview:resultsView.view atIndex:0];

 }


-(void)clearView{
 if(flavourPickerView.view.superview){
  [flavourPickerView.view removeFromSuperview];
 }else if(shakeView.view.superview){
  [shakeView.view removeFromSuperview];
 }else{
  [resultsView.view removeFromSuperview];
 }
}


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
 [self loadFlavourPickerView:nil];
 [super viewDidLoad];
}


- (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 {
 [flavourPickerView release];
 [shakeView release];
 [resultsView release];
    [super dealloc];
}

@end

and here is an example of one of the other views, starting with FlavourPickerView.h


#import 


@interface FlavourPickerView : UIViewController {

}

@end

and FlavourPickerView.m

#import "FlavourPickerView.h"


@implementation FlavourPickerView

- (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 {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (void)dealloc {
    [super dealloc];
}


@end

There is also a nib for the flavourPickerView and I have added subviews into my mainView, with the Class set to FlavourPickerView and nib name is FlavourPickerView.xib.

A: 

Are flavourPickerView, shakeView, and resultsView properly initialized when you add their view to this UIViewController view? Another thing: avoid to call a UIViewController just 'XXXView', try instead 'XXXViewController'

rano
Have added more code to the example above, and noted your valid point about XXXViewController, makes sense.
Charlie Fortune
Have you tried to add the controller programmatically instead of using Interface Builder?
rano