2012-12-13 49 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

相关问题