Tabbar in Second View

2019-01-15 22:42发布

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 created a tabbed bar application, where from my activationView on click of button I am trying to call the tab bar, I am getting an entire black screen.

- (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];}

3条回答
放荡不羁爱自由
2楼-- · 2019-01-15 23:10

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

查看更多
对你真心纯属浪费
3楼-- · 2019-01-15 23:18

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]; 
查看更多
闹够了就滚
4楼-- · 2019-01-15 23:28

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

查看更多
登录 后发表回答