MultipleViewsViewController.h應該是:
#import <UIKit/UIKit.h>
@class FirstViewController;
@class SecondViewController;
@class ThirdViewController;
@interface MultipleViewsViewController : UIViewController {
IBOutlet FirstViewController *firstViewController;
IBOutlet SecondViewController *secondViewController;
IBOutlet ThirdViewController *thirdViewController;
}
//@property (nonatomic, retain) FirstViewController *firstViewController;
//@property (nonatomic, retain) SecondViewController *secondViewController;
//@property (nonatomic, retain) ThirdViewController *thirdViewController;
-(IBAction)loadFirstView:(id)sender;
-(IBAction)loadSecondView:(id)sender;
-(IBAction)loadThirdView:(id)sender;
@end
MultipleViewsViewController.m應該是:
#import "MultipleViewsViewController.h"
#import "FirstViewController.h"
#import "SecondViewController.h"
#import "ThirdViewController.h"
@implementation MultipleViewsViewController
//@synthesize firstViewController;
//@synthesize secondViewController;
//@synthesize thirdViewController;
-(IBAction)loadFirstView:(id)sender{
[secondViewController.view removeFromSuperview];
[thirdViewController.view removeFromSuperview];
[self.view insertSubview:firstViewController.view atIndex:0];
}
-(IBAction)loadSecondView:(id)sender{
[firstViewController.view removeFromSuperview];
[thirdViewController.view removeFromSuperview];
[self.view insertSubview:secondViewController.view atIndex:0];
}
-(IBAction)loadThirdView:(id)sender{
[firstViewController.view removeFromSuperview];
[secondViewController.view removeFromSuperview];
[self.view insertSubview:thirdViewController.view atIndex:0];
}
-(void)dealloc{
[firstViewController release];
[secondViewController release];
[thirdViewController release];
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
firstViewController = [[FirstViewController alloc] init];
secondViewController = [[SecondViewController alloc] init];
thirdViewController = [[ThirdViewController alloc] init];
[self loadFirstView:nil];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
連接您的按鈕(你有沒有在你的項目,這也可能是這個問題做),你就完成了。