Multi-View Application Problem
I want to make multi view application. My goal is when program launch to load first view controller and then when press the button t开发者_如何学运维o load new tab bar controller and dismiss first controller. I try myself and I do firt step, but when tab bar controller is loaded it's appear only small tab without any tabs and old controller doesn't disappear.
This is what I've done:
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];
}
First thing is that in the appDelegate you are adding the view of SwitchClass. But SwitchClass is a UIViewController class and not UIView. so instead of that you should add your SwitchClass as a rootController like this:
self.window.rootViewController = self.switchClass;
If I were you I would just use a tab bar template provided by XCode. It will do it for you automatically.
精彩评论