我想制作多视图应用程序。我的目标是当程序启动时加载第一个视图控制器,然后按下按钮加载新的标签栏控制器并关闭第一个控制器。我尝试自己,我做了第四步,但当加载标签栏控制器时,它只显示没有任何标签的小标签,旧的控制器不会消失。
这就是我所做的:
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];
}
答案 0 :(得分:1)
首先,在appDelegate中添加了SwitchClass的视图。但SwitchClass是一个UIViewController类而不是UIView。所以你应该把你的SwitchClass添加为rootController,如下所示:
self.window.rootViewController = self.switchClass;
如果我是你,我会使用XCode提供的标签栏模板。它会自动为您完成。