2012-05-04 37 views
-1

美好的一天。我嘗試添加新的導航控制器選項卡到我的應用程序。我創建了一個新的選項卡式應用程序(的Xcode 4.2)和的appdelegate寫這將導航控制器添加到選項卡式應用程序

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; 
    // Override point for customization after application launch. 
    UIViewController *viewController1 = [[[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil] autorelease]; 
    UIViewController *viewController2 = [[[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil] autorelease]; 
    NavController *navController = [[[NavController alloc] initWithNibName:@"NavController" bundle:nil] autorelease]; //my controller 
    self.tabBarController = [[[UITabBarController alloc] init] autorelease]; 
    self.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, viewController2, navController, nil]; 
    self.window.rootViewController = self.tabBarController; 
    [self.window makeKeyAndVisible]; 
    return YES; 
} 

NavController.h文件

@interface NavController : UINavigationController 

@end 

以下項目 Structure of progect

的結構,當我運行它的項目給我看空標籤 result 但是在xib文件中我加了標籤和按鈕 What I want in result 可能是我忘記了什麼?

回答

0

初始化您的導航控制器與一些的UIViewController是:

RootViewController *rootViewController = [[[RootViewController alloc] initWithNibName:@"RootViewController" bundle:nil] autorelease]; 
    NavController *navController = [[[NavController alloc] initWithRootViewController: rootViewController] autorelease]; 

這可能會有幫助。

0

我正在分享你關於這個功能我的應用程序代碼,希望這能解決你的問題很容易

在AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 

    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; 
    self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil] autorelease]; 

    UINavigationController *localNavigationController; 

    tabBarController = [[UITabBarController alloc] init]; 

    NSMutableArray *localControllersArray = [[NSMutableArray alloc] initWithCapacity:3]; 

    viewController = [[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil] autorelease]; 
    localNavigationController = [[UINavigationController alloc] initWithRootViewController:viewController]; 
    [localControllersArray addObject:localNavigationController]; 

    AlaramClock *aAlaramClock = [[[AlaramClock alloc] initWithNibName:@"AlaramClock" bundle:nil] autorelease]; 
    localNavigationController = [[UINavigationController alloc] initWithRootViewController:aAlaramClock]; 
    [localControllersArray addObject:localNavigationController]; 


    CurrentTime *aCurrentTime = [[[CurrentTime alloc] initWithNibName:@"CurrentTime" bundle:nil] autorelease]; 
    localNavigationController = [[UINavigationController alloc] initWithRootViewController:aCurrentTime]; 
    [localControllersArray addObject:localNavigationController]; 

    Settings *aSettings = [[[Settings alloc] initWithNibName:@"Settings" bundle:nil] autorelease]; 
    localNavigationController = [[UINavigationController alloc] initWithRootViewController:aSettings]; 
    [localControllersArray addObject:localNavigationController]; 



    tabBarController.viewControllers = localControllersArray; 

    [localControllersArray release]; 


    // Override point for customization after app launch  
    [window addSubview:tabBarController.view]; 
    [window makeKeyAndVisible]; 

    return YES; 
} 

然後添加以下代碼U將得到這個看起來

enter image description here

此後在Every ViewController.m中

添加這個

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) 
    { 
     // Custom initialization 
     [email protected]"Calculate Time"; 
     self.tabBarItem.title = @"Calculate Time"; 
     self.tabBarItem.image=[UIImage imageNamed:@"time_calculate.png"]; 
    } 

    return self; 

} 
+0

試圖在委託中添加這個UINavigationController * localNavigationController; NavController * navController = [[[NavController alloc] initWithNibName:@「NavController」bundle:nil] autorelease]; localNavigationController = [[UINavigationController alloc] initWithRootViewController:navController];但它在initWithRootControllerMethod中崩潰 – nabiullinas

相關問題