IOS:在ios中将viewcontroller添加到自定义TabBar

时间:2016-02-05 05:07:51

标签: ios objective-c uiviewcontroller uitabbar

我正在开展一个聊天项目,我必须使用UITabBar。我在这个项目中使用故事板但是根据要求我在项目中添加了自定义TabBar以将其安装在视图的顶部。 现在问题是Custom TabBar在视图顶部显示良好但是当我点击选项卡时它只显示黑屏而不是显示ViewControllers。 我正在分享我的代码,请告诉我解决方案。

我的.h文件

@interface TabBar : UITabBarController
{

}
@property(strong,nonatomic) UITabBarController *tabbarcontroller;
@property(strong,nonatomic) ChatListVC *chatlist;
@property(strong,nonatomic) ContactListVc *contactlist;
@property(strong,nonatomic) FindfriendsVC *findfriends;

@end

我的.m文件

@implementation TabBar

    - (void)viewDidLoad

        {
            [super viewDidLoad];
            // Do any additional setup after loading the view.
        //    self.navigationItem.hidesBackButton=YES;

            self.title = @"Chat Home Page";

            self.navigationController.navigationBar.backgroundColor = [UIColor blueColor];

            _chatlist = [ChatListVC new];
            _contactlist = [ContactListVc new];
            _findfriends = [FindfriendsVC new];     //initWithNibName:@"FindfriendsVC" bundle:nil];//[FindfriendsVC new];

            self.tabbarcontroller = [[UITabBarController alloc]init];

            self.viewControllers = [NSArray arrayWithObjects:_chatlist,_contactlist,_findfriends, nil];

            //self.navigationController.navigationBar.frame.size.height;
            //UITabBar *tabBar = self.tabBarController.tabBar;
            //CGFloat topBarOffset = self.topLayoutGuide.length;
            self.tabBar.frame =CGRectMake(0,44,self.view.frame.size.width,50);

            UITabBarItem *item0 = [self.tabBar.items objectAtIndex:0];
            UITabBarItem *item1 = [self.tabBar.items objectAtIndex:1];
            UITabBarItem *item2 = [self.tabBar.items objectAtIndex:2];

            item0.title = @"Chat List";
            item1.title = @"Contacts";
            item2.title = @"Find Friends";

            [item0 setFinishedSelectedImage:[UIImage imageNamed:@"chatlist.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"chatlist.png"]];
            [item1 setFinishedSelectedImage:[UIImage imageNamed:@"contacts.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"contacts.png"]];
            [item2 setFinishedSelectedImage:[UIImage imageNamed:@"findfriends.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"findfriends.png"]];

        }

输出的快照是 enter image description here

1 个答案:

答案 0 :(得分:2)

看起来你的视图控制器没有正确初始化。

如果您的视图控制器都在故事板中,请尝试替换

_chatlist = [ChatListVC new];
_contactlist = [ContactListVc new];
_findfriends = [FindfriendsVC new];

使用:

_chatlist = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"ChatListIdentifier"];
_contactlist = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"ContactListIdentifier"];
_findfriends = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"FindfriendsIdentifier"];

标识符字符串是您在故事板中设置的自己的标识符。