zhuber
zhuber

Reputation: 5524

Initializing custom UIViewController

I have TabBar based iPhone application, and in app delegate 2 default view controllers are initialized by apple (if you choose tabbar base app when creating application).

UIViewController *rootViewController = [[tabBarBetFirstViewController alloc] initWithNibName:@"tabBarBetFirstViewController" bundle:nil];
UIViewController *accountViewController = [[tabBarBetSecondViewController alloc] initWithNibName:@"tabBarBetSecondViewController" bundle:nil];

Why this isn't initialized like this:

tabBarBetFirstViewController *rootViewController = [[tabBarBetFirstViewController alloc] initWithNibName:@"tabBarBetFirstViewController" bundle:nil];
tabBarBetSecondViewController *accountViewController = [[tabBarBetSecondViewController alloc] initWithNibName:@"tabBarBetSecondViewController" bundle:nil];

???

Is that the same ? Or it's just those default that are added by apple? If i want to add one more tab will I write:

UIViewController *third = [ThirdViewController alloc].....];

or

ThirdViewController *third = [ThirdViewController alloc]....];

Of course at the end I have:

self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:rootViewController, accountViewController, third, nil];

Upvotes: 1

Views: 151

Answers (5)

herzbube
herzbube

Reputation: 13378

ThirdViewController is a subclass of UIViewController, so you can write both. But if you later want to use the variable third to invoke methods that are specific to ThirdViewController, then you should use

ThirdViewController *third = [ThirdViewController alloc]....];

Summing it up: In this simple scenario there is no single right way of "doing it". The important lesson to take from this question (if it wasn't clear already) is to understand why you can assign a ThirdViewController instance to a UIViewController variable (because of the subclassing relationship).

Upvotes: 2

Yunus Nedim Mehel
Yunus Nedim Mehel

Reputation: 12369

In this case I do not see any difference, I would prefer doing it your way. But in a situation similar to the one below, Apple's way seems better:

UIViewController *vc;

if ( some_case ){

    vc = [YourViewController1 alloc]// ...;
    [ (YourViewController1 *) vc doSomeThing]; // You might need to use casting for instance messages
    //...
}

else {

    vc = [YourViewController2 alloc]//...;
}

[self.navigationController pushViewController:vc animated:YES];
[vc release];

Upvotes: 0

Nitin Alabur
Nitin Alabur

Reputation: 5812

1) If you want to make use of any instance methods or properties in your ThirdViewController, then you must use

ThirdViewController *third = [ThirdViewController alloc]....];

2) If you dont have a need to do so, you can use

UIViewController *third = [ThirdViewController alloc]....]; // it'd make no difference

To be on a safer side, imo, first case is a good practice.

Upvotes: 0

dmason82
dmason82

Reputation: 399

It depends, if you have a view controller that you want to have a custom interface, you will want it to be a subclass of UIViewController. If ThirdViewController is a subclass of UIViewController then that code that you stated here:

ThirdViewController *third = [ThirdViewController alloc]....];

Would produce the desired result. Apple's approach is just for a generic View Controller without any properties, so ideally you would want all of your tabs to be UIViewController subclasses.

Upvotes: 0

dasdom
dasdom

Reputation: 14063

You use the

ThirdViewController *third = [ThirdViewController alloc]....];

approach. Don't know why Apple uses the other approach. I this easy example it doesn't make any difference. But when you have properties you want to set it's better to use the class name.

Upvotes: 0

Related Questions