多视图应用程序问题

问题描述:

我想制作多视图应用程序。我的目标是在程序启动时加载第一个视图控制器,然后按下按钮加载新的标签栏控制器并关闭第一个控制器。我尝试自己,并且我会一步一步地做,但是当标签栏控制器加载时,它只会出现小标签,没有任何标签,旧控制器也不会消失。多视图应用程序问题

这是我做了什么:

SwitchAppelegate

-- Header file -- 
#import <UIKit/UIKit.h> 

@class SwitchClass; 

@interface SwitchAppDelegate : NSObject <UIApplicationDelegate> { 
    SwitchClass *switchClass; 
} 

@property (nonatomic, retain) IBOutlet UIWindow *window; 
@property (nonatomic, retain) IBOutlet SwitchClass *switchClass; 

@property (nonatomic, retain, readonly) NSManagedObjectContext *managedObjectContext; 
@property (nonatomic, retain, readonly) NSManagedObjectModel *managedObjectModel; 
@property (nonatomic, retain, readonly) NSPersistentStoreCoordinator *persistentStoreCoordinator; 

- (void)saveContext; 
- (NSURL *)applicationDocumentsDirectory; 

@end 

-- Implementation file -- 
#import "SwitchAppDelegate.h" 

@implementation SwitchAppDelegate 


@synthesize window=_window; 

@synthesize managedObjectContext=__managedObjectContext; 

@synthesize managedObjectModel=__managedObjectModel; 

@synthesize persistentStoreCoordinator=__persistentStoreCoordinator; 

@synthesize switchClass; 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    // Override point for customization after application launch. 
    [self.window addSubview:[switchClass view]]; 

    [self.window makeKeyAndVisible]; 
    return YES; 
} 

SwitchClass

-- Header file -- 

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

@interface SwitchClass : UIViewController { 
    BlueClass *blueClass; 
} 

@property (nonatomic, retain) IBOutlet BlueClass *blueClass; 

@end 

-- Implementation file -- 

#import "SwitchClass.h" 
#import "BlueClass.h" 

@implementation SwitchClass 

@synthesize blueClass; 

-(void) viewDidLoad { 
    BlueClass *blue = [[BlueClass alloc] initWithNibName:@"BlueClass" bundle:nil]; 
    self.blueClass = blue; 

    [self.view insertSubview:blue.view atIndex:0]; 
    [blue release]; 

    [super viewDidLoad]; 
} 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; 
} 

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

BlueClass

-- Header file -- 

@class RedClass; 

@interface BlueClass : UIViewController { 
    RedClass *redClass; 
} 

@property (nonatomic, retain) IBOutlet RedClass *redClass; 

-(IBAction) switch: (id) sender; 

@end 

-- Implementation file -- 

#import "BlueClass.h" 
#import "RedClass.h" 

@implementation BlueClass 

@synthesize redClass; 

-(IBAction) switch: (id) sender { 
    RedClass *blue = [[RedClass alloc] initWithNibName:@"RedClass" bundle:nil]; 
    self.redClass = blue; 
// self.window.rootViewController = self.tabBarController; 
    [self.view addSubview:blue.view]; 
    [blue release]; 

    [super viewDidLoad]; 
} 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; 
} 

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

RedClass

-- Header file -- 

#import <UIKit/UIKit.h> 

@interface RedClass : UITabBarController { 

} 

@end 

-- Implementation file -- 

#import "RedClass.h" 

@implementation RedClass 


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; 
} 

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

的第一件事是,在的appDelegate要添加SwitchClass的视图。但是SwitchClass是一个UIViewController类,而不是UIView。所以不是说你应该添加您的SwitchClass作为这样的rootController:

self.window.rootViewController = self.switchClass; 

如果我是你,我只想用通过的XCode提供了一个标签栏模板。它会自动为你做。

+0

谢谢我会试试这个 – 2011-04-21 20:50:22