2011-04-21 103 views
0

我想製作多視圖應用程序。我的目標是在程序啓動時加載第一個視圖控制器,然後按下按鈕加載新的標籤欄控制器並關閉第一個控制器。我嘗試自己,並且我會一步一步地做,但是當標籤欄控制器加載時,它只會出現小標籤,沒有任何標籤,舊控制器也不會消失。多視圖應用程序問題

這是我做了什麼:

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]; 
} 

回答

1

的第一件事是,在的appDelegate要添加SwitchClass的視圖。但是SwitchClass是一個UIViewController類,而不是UIView。所以不是說你應該添加您的SwitchClass作爲這樣的rootController:

self.window.rootViewController = self.switchClass; 

如果我是你,我只想用通過的XCode提供了一個標籤欄模板。它會自動爲你做。

+0

謝謝我會試試這個 – 2011-04-21 20:50:22