Hacer sengul Akac
Hacer sengul Akac

Reputation: 683

tab bar item title programatically not showing in objective c

I try create tab bar controller programatically, it works but I can not set title to ta bar items. I can not see title when I running my application. My code is here. Please help me, what is the problem?

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.

myTabBarController = [[UITabBarController alloc] init];        
view2Controller = [[testView alloc] init];   
view3Controller = [[testView2 alloc] init];   
view4Controller = [[testView3 alloc] init];   
view5Controller = [[testView4 alloc] init];   
view6Controller = [[testView5 alloc] init];   


myTabBarController.viewControllers = [NSArray arrayWithObjects: view2Controller, view3Controller,view4Controller,view5Controller,view6Controller,nil]; 
 UITabBarItem *tabItem = [[[myTabBarController tabBar] items] objectAtIndex:1];
 [tabItem setTitle:@"theTitle"];

[self.view addSubview:myTabBarController.view];    
myTabBarController.selectedIndex=0;

}

Upvotes: 1

Views: 4587

Answers (6)

Shockki
Shockki

Reputation: 61

override func viewDidLoad() {
    super.viewDidLoad()

    DispatchQueue.main.async {
        self.tabBar.items?[0].title = "Title"
    }

}

OR

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    self.tabBar.items?[0].title = "Title"

}

Upvotes: 0

user1909130
user1909130

Reputation:

use this code

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title=@"FREE BOOKS";
        self.tabBarItem.image = [UIImage imageNamed:@"first"];
    }
    return self;
}

Upvotes: 2

Siba Prasad Hota
Siba Prasad Hota

Reputation: 4789

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {

        // self.title = @"My Title";
    }

    self.title = @"My Title";
    return self;
}

Use above code in every subviews of tabbar controllers.

This may be help.

Upvotes: 2

Sailesh
Sailesh

Reputation: 26207

You can set the title of UIViewController, which reflects when it is pushed in UINavigationController or a UITabBarController. But you should set the title before putting it inside any of them.

init is generally a good place to set the title.

- (id)init {
    // ... other code including check for self
    self.title = @"My Title";
    return self;
}

Upvotes: 2

UPT
UPT

Reputation: 1480

Here is the tutorial from the scratch which should help you to achieve the exact functionality you want to achieve. Download the source code and run it directly and if title are correct the you can follow the tutorial to get the proper understanding of the flow of UITabBarController.

Upvotes: 0

JOA80
JOA80

Reputation: 527

Set the title of ur viewcontroller in side viewcontroller's viewwillappear/disappear and it will be displayed in ur tabbaritem.

Upvotes: 2

Related Questions