以编程方式将Tab Bar Controller添加到当前App Flow [英] Add a Tab Bar Controller Programmatically to current App Flow

查看:82
本文介绍了以编程方式将Tab Bar Controller添加到当前App Flow的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我当前的App Flow中添加一个标签栏控制器。目前我有一个带有按钮的页面,在点击时打开一个带有Web视图的新视图控制器,用户登录,登录后我想带他到他的主页,导航栏上有他的名字和右边的注销按钮。主页还应该有一个带有3个不同选项卡的标签栏。
我可以从webview加载主页视图并获取导航栏。但我无法添加tabBar并使其正常工作。我很困惑在哪里添加添加TabBar的代码。我使用下面的代码添加标签栏 -

I want to add a tab Bar Controller to my current App Flow. Currently I have a page with a button which on clicking opens a new viewcontroller with a web view where the user logs in and after login I want to take him to his home Page where the navigation bar has his name and a logout button in the right. The home page should also have a tab bar with 3 different tabs. I am able to load the home page view from the webview and get the navigation bar. But I am unable to add the tabBar and get it working. I am confused as to where to add the code for adding TabBar. I am using the below code to add tab bar -

UITabBarController *tabBar = [[UITabBarController alloc] init];

HomeViewController *home = [[PPHomeViewController alloc] initWithUserName:[self.userInfo objectForKey:@"name"] Email:[self.userInfo objectForKey:@"email"] Phone:[self.userInfo objectForKey:@"phone_number"]];
home.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemFavorites tag:1];
UINavigationController *homeNavController = [[UINavigationController alloc]initWithRootViewController:home];

RequestViewController *req = [[RequestMoneyViewController alloc]init];
req.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemFavorites tag:2];
UINavigationController *reqNavController = [[UINavigationController alloc]initWithRootViewController:req];

UIViewController *thirdViewController = [[UIViewController alloc]init];
thirdViewController.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemFavorites tag:3];
UINavigationController *thirdNavController = [[UINavigationController alloc]initWithRootViewController:thirdViewController];

UIViewController *fourthViewController = [[UIViewController alloc]init];
thirdViewController.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemFavorites tag:3];
UINavigationController *fourthNavController = [[UINavigationController alloc]initWithRootViewController:fourthViewController];

tabBar.viewControllers = [[NSArray alloc] initWithObjects:homeNavController, reqNavController, thirdNavController, fourthNavController, nil];
tabBar.delegate=self;
tabBar.selectedIndex=0;

UIImageView *homeImg=[[UIImageView alloc]initWithFrame:CGRectMake(0, 432, 80, 49)];
homeImg.tag=11;
homeImg.image=[UIImage imageNamed:@"footer"];

UIImageView *reqImg=[[UIImageView alloc]initWithFrame:CGRectMake(81, 432,80, 49)];
reqImg.tag=12;
reqImg.image=[UIImage imageNamed:@"footer"];

UIImageView *sendImg=[[UIImageView alloc]initWithFrame:CGRectMake(162, 432,80, 49)];
sendImg.tag=13;
sendImg.image=[UIImage imageNamed:@"footer"];

UIImageView *localImg=[[UIImageView alloc]initWithFrame:CGRectMake(243, 432, 80, 49)];
localImg.tag=14;
localImg.image=[UIImage imageNamed:@"footer"];

[tabBar.view addSubview:homeImg];
[tabBar.view addSubview:reqImg];
[tabBar.view addSubview:sendImg];
[tabBar.view addSubview:localImg];

[[[UIApplication sharedApplication]keyWindow]addSubview:tabBar.view];

目前我已将上述代码放在ViewController TabViewController的viewDidLoad中,该窗口扩展了UITabBarController。在我的webView控制器中,我输入了以下代码 -

Currently I have put the above code in the viewDidLoad of a ViewController TabViewController which extends UITabBarController. In my webView controller I have put the following code -

TabViewController *tab=[[TabViewController alloc] init];
tab.userInfo=userInfo;
[self presentViewController:tab animated:YES completion:nil];

但是,只要点击已经打开的标签之外的任何标签,应用程序就会崩溃。
请帮忙。

But the app crashes as soon as I click any tab other than the one already open. Please Help.

推荐答案

我过去的做法是创建一个 UITabBarController 包含上面所有 tabBar 创建代码的子类。

The way I've done this in the past is to create a UITabBarController subclass that contains all of the tabBar creation code you have above.

然后使用 UINavigationController tabBar 子类推送到屏幕。

Then use your UINavigationController to push the tabBar subclass to the screen.

以下是我的 UITabBarController 子类的示例:

Here's a sample of my UITabBarController subclass:

- (void)viewDidLoad {
    [super viewDidLoad];

    UIViewController *view1 = [[UIViewController alloc] init];
    UIViewController *view2 = [[UIViewController alloc] init];
    UIViewController *view3 = [[UIViewController alloc] init];

    NSMutableArray *tabViewControllers = [[NSMutableArray alloc] init];
    [tabViewControllers addObject:view1];
    [tabViewControllers addObject:view2];
    [tabViewControllers addObject:view3];

    [self setViewControllers:tabViewControllers];
    //can't set this until after its added to the tab bar
    view1.tabBarItem = 
      [[UITabBarItem alloc] initWithTitle:@"view1" 
                                    image:[UIImage imageNamed:@"view1"] 
                                      tag:1];
    view2.tabBarItem = 
      [[UITabBarItem alloc] initWithTitle:@"view2" 
                                    image:[UIImage imageNamed:@"view3"] 
                                      tag:2];
    view3.tabBarItem = 
      [[UITabBarItem alloc] initWithTitle:@"view3" 
                                    image:[UIImage imageNamed:@"view3"] 
                                      tag:3];      
}

这篇关于以编程方式将Tab Bar Controller添加到当前App Flow的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆