开发者

Change TabBarItem text in WindowBased app

开发者 https://www.devze.com 2023-02-14 03:47 出处:网络
I create window based app (call it TabBar). In TabBarAppDelegate I create two UIViewControllers and one UITabBarController.

I create window based app (call it TabBar). In TabBarAppDelegate I create two UIViewControllers and one UITabBarController.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
    UIViewController *vc1 = [[UIViewController alloc]
initWithNibName:@"MyFirstViewController" bundle:nil];

    UIViewController *vc2 = [[UIViewController alloc]
initWithNibName:@"MySecondViewController" bundle:nil];

    NSArray *contr = [NSArray arrayWithObjects:vc1, vc2, nil];
    UITabBarController *tbc = [[UITabBarController alloc] init];
    tbc.viewControllers = contr;
    [self.window addSubview:tbc.view];
    [self.window makeKeyAndVisible];
    [vc1 rele开发者_如何学Goase]; [vc2 release];
    return YES;
}

In MyFirstViewController.xib I create uilabel (using IB for test that view is loaded) and for MySecondViewController.xib too.

And app build and run successful, but uitabbaritem - without image and title. I tried to add in MyFirstViewController.m this lines for set title and image of MyFirstViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        UITabBarItem *item = [[UITabBarItem alloc] 
initWithTitle:@"first" image:[UIImage imageNamed:@"first.png"] tag:0];

        self.tabBarItem = item;
        [item release];
    }
    return self;
}

But first tabbaritem isn't change. Help me please. I don't understand what I'm doing wrong.


The answer is

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
    MyFirstViewController *vc1 = [[MyFirstViewController alloc]
initWithNibName:@"MyFirstViewController" bundle:nil];

    MySecondViewController *vc2 = [[MySecondViewController alloc]
initWithNibName:@"MySecondViewController" bundle:nil];

    NSArray *contr = [NSArray arrayWithObjects:vc1, vc2, nil];
    UITabBarController *tbc = [[UITabBarController alloc] init];
    tbc.viewControllers = contr;
    [self.window addSubview:tbc.view];
    [self.window makeKeyAndVisible];
    [vc1 release]; [vc2 release];
    return YES;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号