为什么这个简单的视图切换项目不起作用?

时间:2011-08-11 22:53:12

标签: xcode view project switching

我正在学习如何在Xcode中编写应用程序。我正在用一本书来指导我。不幸的是,这本书是用Xcode 3编写的,我正在使用Xcode 4.

现在到目前为止还没有任何问题,但是这个项目不起作用,我根本就没有得到它,因为它看起来很有道理。

该项目的目标是使用视图控制器在三个视图之间切换。

有谁可以看一看,看看我做错了什么?

以下是整个项目:http://www.2shared.com/file/CKO6ACzg/MultipleViews.html

PS:我知道现在,视图将叠加在一起,并且当您单击一个新按钮时视图不会被清除。

1 个答案:

答案 0 :(得分:0)

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.
}

连接你的按钮(你在项目中没有做过,也可能是问题),你已经完成了。