2012-12-13 82 views
0

我在我的應用中有一個激活頁面,每個用戶都必須激活該頁面才能激活該應用。 一旦該應用程序被激活,用戶移動到標籤欄視圖。第二視圖中的Tabbar

我已經創建了一個標籤欄應用程序,從我的activationView上點擊按鈕我試圖調用標籤欄,我得到一個完整的黑色屏幕。

- (IBAction)moveToActivateView:(id)sender { 
    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; 
    UITabBarController *tabBarController = [[UITabBarController alloc]init]; 
    [self.view addSubview:tabBarController.view]; 
    appDelegate.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 

    UIViewController *viewController1 = [[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil]; 
    UIViewController *viewController2 = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil]; 

    self.tabBarController.viewControllers = @[viewController1, viewController2]; 
    appDelegate.window.rootViewController = self.tabBarController; 
    [appDelegate.window makeKeyAndVisible];} 
+0

誰是'自我'在這裏? –

+0

是你的問題解決? – Rajneesh071

回答

2

嘿讓你tabBarController在的appDelegate屬性,並指定所有的ViewController有再打電話給你tabBarController從yourViewController

在AppDelegate.h

@property (nonatomic, retain) IBOutlet UINavigationController *navigationController; 
@property (retain, nonatomic) IBOutlet UITabBarController *tabBarController; 

在AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 

    [self makeTabBar]; 

    [self addInitialVIew]; 

    [self.window makeKeyAndVisible]; 

    return YES; 
} 


-(void)makeTabBar 
{  
    tabBarController = [[UITabBarController alloc] init]; 
    tabBarController.delegate=self; 
    FirstViewController *firstVC =[[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil];  

    UINavigationController *firstNC = [[UINavigationController alloc] initWithRootViewController:firstVC]; 
    [email protected]"Profile"; 
    firstVC.tabBarController.tabBar.tag = 0; 


    SecondViewController *secondVC = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil]; 

    UINavigationController *SecondNavController = [[UINavigationController alloc] initWithRootViewController:secondVC];  
    SecondNavController.tabBarItem.title = @"Search"; 
    secondVC.tabBarController.tabBar.tag = 1; 


    NSArray *viewControllers =[[NSArray alloc]initWithObjects:firstNC,SecondNavController, nil]; 
    [tabBarController setViewControllers:viewControllers animated:NO]; 
} 

-(void) addInitialVIew 
{  
    InitialViewController *initialViewController = [[InitialViewController alloc]initWithNibName:@"InitialViewController" bundle:nil]; 
    navigationController = [[UINavigationController alloc]initWithRootViewController:ViewController]; 
    [self.window addSubview:navigationController.view]; 
} 

現在InitialViewController可以添加yourTabBar和刪除InitialViewController

- (IBAction)switchToTabBarBtnPress:(id)sender 
{ 
    AppDelegate *appdelegte =(AppDelegate*)[[UIApplication sharedApplication]delegate]; 

    [[[appdelegte navigationController] view]removeFromSuperview]; 

    [[appdelegte window]addSubview:[[appdelegte tabBarController]view]]; 

    [[appdelegte tabBarController]setSelectedIndex:0]; 
} 

,並按照我的回答

Answer

0

你有沒有意識到,你的本地varialbe tabBarController是不相同,但排序的隱藏屬性self.tabBarController?您創建一個新的UITabBarCotnroller並將其分配給您的本地變量tabBarController,該變量只能從此方法訪問。然後你操縱(添加新創建的視圖控制器)等self.tabBarControllerSelf.tabBarController可能很容易在這裏或其他任何東西。但這不是你剛剛創建的UITabBarController

它是self.tabBarController(所以可能無)您分配給窗口的內容爲rootViewController

您確實將tabBarController's視圖添加爲self.view的子視圖。除此之外,我不知道自己的實際是什麼,我不認爲手動將tabBar的視圖添加爲子視圖是非常必要的。設置根視圖控制器(正確)應該足夠了

0

您想要創建動態的tabbar應用程序來解決您的問題。所以你只需創建基於視圖的應用程序。在視圖控制器viewdidload方法把這些代碼

tabbar1 = [[UITabBarController alloc] init]; 

    artist_tab_obj = [[artist_tab alloc] initWithNibName:@"artist_tab" bundle:nil]; 

    UINavigationController *tabItem1 = [[[UINavigationController alloc] initWithRootViewController: artist_tab_obj] autorelease]; 
    [email protected]"Artist"; 
    tabItem1.tabBarItem.image=[UIImage imageNamed:@"Icon1.png"]; 
    music_tab_obj = [[music_tab alloc] initWithNibName:@"music_tab" bundle:nil]; 

    UINavigationController *tabItem2 = [[[UINavigationController alloc] initWithRootViewController: music_tab_obj] autorelease]; 

    [email protected]"Music"; 
    tabItem2.tabBarItem.image=[UIImage imageNamed:@"Icon2.png"]; 

    shout_tab_obj = [[shout_tab alloc] initWithNibName:@"shout_tab" bundle:nil]; 

    UINavigationController *tabItem3 = [[[UINavigationController alloc] initWithRootViewController: shout_tab_obj] autorelease]; 

    [email protected]"Shout"; 
    tabItem3.tabBarItem.image=[UIImage imageNamed:@"Icon3.png"]; 
    schedule_tab_obj = [[schedule_tab alloc] initWithNibName:@"schedule_tab" bundle:nil]; 

    UINavigationController *tabItem4 = [[[UINavigationController alloc] initWithRootViewController: schedule_tab_obj] autorelease]; 

    [email protected]"Schedule"; 
    tabItem4.tabBarItem.image=[UIImage imageNamed:@"Icon4.png"]; 
    follow_tab_obj = [[follow_tab alloc] initWithNibName:@"follow_tab" bundle:nil]; 

    UINavigationController *tabItem5 = [[[UINavigationController alloc] initWithRootViewController: follow_tab_obj] autorelease]; 
    [email protected]"Follower"; 
    tabItem5.tabBarItem.image=[UIImage imageNamed:@"Icon5.png"]; 


    tabbar1.viewControllers = [NSArray arrayWithObjects:tabItem1, tabItem2,tabItem3,tabItem4,tabItem5,nil]; 

在用戶接受是按鈕操作調用此代碼。

[self.view insertSubview:tabbar1.view belowSubview: artist_tab_obj.view]; 
tabbar1.selectedIndex=1; 
[self presentModalViewController:tabbar1 animated:YES]; 
+0

我試着實現你的代碼,我得到了一個錯誤 '在'UIViewController *'類型的對象上找不到Property'viewControllers'請你也可以共享相應的h文件。 – onkar

相關問題