Tabbar in Second View

前端 未结 3 1389
北恋
北恋 2020-12-07 06:04

I have an activation page in my app, which is mandatory for every user to activate the app. Once the app is activated, the user moves to the tabbed bar view.

I have

相关标签:
3条回答
  • 2020-12-07 06:42

    You want to create dynamic tabbar application to resolve your problem. So you just create the view based application. in the view controller viewdidload method put these code

    tabbar1 = [[UITabBarController alloc] init];
    
        artist_tab_obj = [[artist_tab alloc] initWithNibName:@"artist_tab" bundle:nil];
    
        UINavigationController *tabItem1 = [[[UINavigationController alloc] initWithRootViewController: artist_tab_obj] autorelease];
        tabItem1.title=@"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];
    
        tabItem2.title=@"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];
    
        tabItem3.title=@"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];
    
        tabItem4.title=@"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];
        tabItem5.title=@"Follower";
        tabItem5.tabBarItem.image=[UIImage imageNamed:@"Icon5.png"];
    
    
        tabbar1.viewControllers = [NSArray arrayWithObjects:tabItem1, tabItem2,tabItem3,tabItem4,tabItem5,nil]; 
    

    In user acceptation is yes button action call this code.

    [self.view insertSubview:tabbar1.view belowSubview: artist_tab_obj.view];
    tabbar1.selectedIndex=1;
    [self presentModalViewController:tabbar1 animated:YES]; 
    
    0 讨论(0)
  • 2020-12-07 06:48

    Hey make your tabBarController's property in appDelegate and assign all ViewController there then call your tabBarController from yourViewController

    in AppDelegate.h

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

    in 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];
        firstNC.tabBarItem.title=@"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];
    }
    

    Now in InitialViewController you can add yourTabBar and remove 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];
    }
    

    And Follow my answer

    Answer

    0 讨论(0)
  • 2020-12-07 06:53

    Did you realize that your local varialbe tabBarController is not identical but sort of hides the property self.tabBarController? You create a new UITabBarCotnroller and assign it to your local variable tabBarController, which is accessible from this method only. Then you manipulate (add newly created view controllers) etc to self.tabBarController. Self.tabBarController could easily be nil here or anything else. But it is not the very UITabBarController that you just created.

    And it is self.tabBarController (so probably nil) what you assign to the window as rootViewController.

    You do add the tabBarController's view as subview to self.view. Besides that I do not know what self acutally is, I don't think is it really nessessary to manually add the tabBar's view as subview. Setting the root view controller (properly) should be enough

    0 讨论(0)
提交回复
热议问题