简体   繁体   中英

Point of Reference: Replacing the Root View Controller / App Delegate?

I'm new to iOS development, obviously, and I'm running into a bit of an issue with many of the tutorials that I find online. While I understand the majority of the code that is going into these programs, I cannot figure out how to translate this one fact, which is probably simple.

Many tutorials either use the Navigation-based template or View-based template, but I would like to try building tab bar applications. Tutorials either use the App Delegate or rootViewController (being the navigation controller), but since my tab bar is my rootViewController, I always run into an issue. I'm also unable to use the applicationDidFinishLaunching method in most of my code, because it technically only applies to the first tab at launch.

These are my questions:

  1. Is there a way to "translate" these files into new classes (for example, creating an instance of NSObject for the App Delegate code for each individual tab or creating instances of UINavigationViewController)?

  2. If I can create the instance of NSObject, how do I ensure that the code links up to the objects that I create in my viewController?

  3. If I can create a file that uses UINavigationController, how do I trick the program into temporarily allowing the Nav Controller to be the rootViewController.

Many thanks in advance!

Right off the top of my head, I can only answer number 3 for you, you could do that in one of two ways that I am aware, and if anyone sees either of these is incorrect or bad practice, I hope someone corrects me, as this is what I have been doing.

[appDelegate.window addSubview:appDelegate.newRootViewController.view];
appDelegate.window.rootViewController = appDelegate.newRootViewController; 

or

[self.navigationController pushViewController:appDelegate.newRootViewController animated:YES];

Both should accomplish the same thing, though if you can help it, the second version is ideal. Its also worth noting that for the first one, there is no tricking, you are actually setting the rootViewController of your app to something different.

I'll do some digging and see if I can answer any of your other questions for you too.

Edit: So after re-reading and thinking a bit more about it, I think the other two questions can be answered by maybe clarifying a tab bar application. Unfortunately, I'm fairly new to iOS as well, and I haven't had to opportunity to create a tab bar application, so I don't want to give you incorrect info. I would recommend checking out http://www.techotopia.com/index.php/Creating_an_iOS_4_iPhone_Multiview_Application_using_the_Tab_Bar_%28Xcode_4%29 and hopefully it can give you a little bit of a better idea of how Tab Bar Apps work. I've been using that eBook along with another from that site combined with Ray Wenderlich's tutorials to teach myself.

Anyways, I hope this helps to some extent, if you want me to try to clarify or go into detail on anything just comment and I'll see if I can help.

Good Luck!

-Karoly

You're misunderstanding how the app delegate works.

It's the delegate for the application, not a controller. Your controllers may be loaded from a xib by the time applicationDidFinishLaunching is called, but there's no connection between the two events.

applicationDidFinishLaunching is just the place where you do final setup before the app is ready for use.

If you don't instantiate your tab bar controller in your main xib file, you can instantiate it here, then instantiate all of it's controllers and add them to to the tab bar controller. While you're doing that, you can load plists, set properties on the controllers, etc.

If you do instantiate your tab bar controller in your main xib file, you can still get access to its controllers here. You can edit those controllers or throw them out and create new ones. You can even throw out your tab bar controller and switch to a navigation controller.

I can't answer questions 1 and 2 because the assumptions behind them are invalid.

Rather than ask how you work around perceived problems with the app delegate and tab bar controllers, describe what you're trying to accomplish.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM