LaurelS
LaurelS

Reputation: 492

IOS tab bar names show up for other view controllers, but not for Table View Controller?

I hope you can tell me what I'm doing wrong. Also, apologies, I'm working on learning to get the code formatted right in the question.

I'm sure that will be intuitive once I know how it works:-)

Thank you.

I am working on creating a sample app in the style of iPhotos - one that has 4 tab bars at the bottom of the window and where the first view that comes up is a UITableViewController, it comes up with those directional arrows at the end of each row/instance, user can click on any of them and get more detail about that instance. That first view is called ItemsViewController. It has a blank tab bar button associated with it in the tab bar, and I can return to it. The other 3 tab bar items are all regular UIViewController's, in my sample they each just bring up an image and that's all they do (for now).

I have finally managed to get all 3 UIViewController and the one UITableViewController to exist and work in the same app. (hooray!) and I can get names to show up on each of the 3 tab bar items that are not the table view controller. But I cannot get the UITableViewController.m to display a name on it's tab bar item in the tab bar.

....... This is the code I use in the app delegate method ....

`- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UITabBarController *tabBar = [[UITabBarController alloc] init];


ChairGarden* vc1 = [[ChairGarden alloc] init];

JSerra* vc2 = [[JSerra alloc] init];


Angel* vc3 = [[Angel alloc] init];

// Create aN ItemsViewController
  ItemsViewController *itemsViewController = [[ItemsViewController alloc] init];

// Create an instance of a UINavigationController
// its stack contains only itemsViewController
UINavigationController *vc4 = [[UINavigationController alloc]initWithRootViewController:itemsViewController];

   NSArray* controllers = [NSArray arrayWithObjects:vc4, vc1, vc2, vc3, nil];

   tabBar.viewControllers = controllers;

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

// Place navigation controller's view in the window hierarchy

[[self window] setRootViewController:tabBar];

self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}

`

..... This is a snippet of code I use in one of the regular UIViewController's that specifies a tab bar title. I see the title 'Chair Garden' on the tab bar, and it brings up the right image. The other two regular ones work the same way

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // get tab bar item
        UITabBarItem *tbi = [self tabBarItem];
        [tbi setTitle:@"Chair Garden"];

    }
    return self;
}

...... I have tried to make a tab bar item with a title for the first view that comes up - my UITableViewController called 'ItemsViewController', but it doesn't come up. I've tried putting it in both the init and the initWithNibName:bundle (did I specify that method name right?) (I also tried them separately). In both cases my NSLog statements show me I was there, but the tab doesn't get a title on it

@implementation ItemsViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        NSLog (@"Getting to initWithNibName in ItemsViewController");

        // get tab bar item
        UITabBarItem *tbi = [self tabBarItem];
        // This dosn't show up
        [tbi setTitle:@"Table View"];

    }
    return self;
}

- (id)init 
{
    // Call the superclass's designated initializer
    self = [super initWithStyle:UITableViewStyleGrouped];
    if (self) {
        NSLog (@"Getting to init in ItemsViewController");
        // get tab bar item
        UITabBarItem *tbi = [self tabBarItem];
        // This dosn't show up EITHER
        [tbi setTitle:@"HOME"];

Upvotes: 0

Views: 950

Answers (2)

Phanidhar
Phanidhar

Reputation: 1

write self.title=@"some name";

in viewDidLoad method instead of initWithNibName

Upvotes: 0

rdelmar
rdelmar

Reputation: 104082

I'm not sure why this works differently for a UITableViewController vs a UIViewController (I was able to duplicate what you were seeing). However, you can set the title of the table view controller itself in the init method, and that will show up on the tab bar item (by default, a tab bar item displays the controller's title).

Upvotes: 1

Related Questions