1

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];
1
  • just to note, i was using the apple's version, UIViewController *third = [ThirdViewController alloc]...]; and its working... but just want to know whats the right way of doing it. Commented Dec 27, 2012 at 16:29

5 Answers 5

2

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).

Sign up to request clarification or add additional context in comments.

1 Comment

ohh, that makes sense :D . well, i use it just in app delegate (like i wrote) so in that case dont invoke any methods on that, just add them to tabbarcontroller... thx
0

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.

Comments

0

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.

Comments

0

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.

Comments

0

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];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.