Loading a different XIB file after Application launches in TabBar Application Template - iphone

I'm creating an app where user needs to login first and only after that they can view anything in the app. To achieve that, I have created a new XIB file with the name 'AuthView'.
I know I need to put the code inside 'applicationDidfinishLaunching' method, but I don't know what code do I need to put inside it.
I'm developing the app using 'Tab Bar Application' template.

By default the end of the ApplicationDidFinishLaunchingWithOptions method looks like this:
[window addSubview:tabcontroller.view];
[window makeKeyAndVisible];
return YES;
Adding a viewController that appears over everything first is easy. First, add the viewController to your implementation (using the real name of your controller, obviously):
#import "InitialScreenViewController.h"
Then modify the end of your ApplicationDidFinishLaunchingWithOptions method by adding two lines as shown:
[window addSubview:tabcontroller.view];
initialScreenViewController = [[InitialScreenViewController alloc] init];
[window addSubview:initialScreenViewController.view];
[window makeKeyAndVisible];
return YES;
Once you've verified the login (or whatever you want to do with the initial screen) simply dismiss it within the initial screen viewController like this:
[self.parentViewController.view setHidden:YES];
This wil allow you to show it again later if need be, like if you add logout and re-login functionality.

As you're using the Tab bar application template, your UIApplicationDelegate file should have
a UITabBarController ivar named tabBarController.
You'll need to remove this ivar (and also remove it from your mainWindow XIB file).
Once this is done, go back to applicationDidfinishLaunching method and add the authentificate view in the windows like this :
MyAuthentificateViewController * viewController = [[[MyAuthentificateViewController alloc]initWithNibName:#"XIBFilename" bundle:nil] autorelease];
[windows addSubview:myAuthentificateViewController.view]];

Better you show your authenticate view first. In another view, place your tabbar controller.

Related

NavigationController and Modal Views

I am a newbie to iOS world and have started building custom code on top of a templated code.
So excuse me for the obvious.
The View chain starts with a MainWindow.xib which contains a App Delegate Object, a Window Object and Application ViewController. I dont understand why those objects are needed over there. But what I understand, I need to mention starting ViewController in the "Nib Name" Property to initiate my custom View Controller (called "EmptyViewController"). Its a dummy view controller, just there to avoid crash to happen as a result of missing valid viewcontroller.
I initiate a separate Modal View Controller(MainViewController) inside didFinishLaunchingWithOptions.
Code for initiating modal View Controller --
self.window.rootViewController = self.viewController;
mainView = [[MainViewController alloc] initWithNibName:#"MainViewController" bundle:nil];
// present the viewcontroller
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:mainView];
[self.viewController presentModalViewController:navController animated:NO];
// release it, because it's retained as modalViewController
[navController release];
I do not put this MainViewController inside MainWindow.xib as I want to have navigation at the root of MainViewController.
Inside MainViewController, I push HelpViewController when "help" button is pressed.
But HelpViewController does not show any navigation bar. I do not understand why?
Code for Pushing Navigation bar --
HelpViewController *helpVC = [[HelpViewController alloc] init];
[self.navigationController pushViewController:helpVC animated:YES];
[helpVC release];
So I would like to understand --
1) Why is MainWindow.xib needed? Can I remove it? (Note: I tried to remove it, but then I get blank screen)
1.a) Why are all the controls/objects App Delegate Object, a Window Object and Application ViewController objects needed?
2) Why doesnt HelpViewController show Navigation bar?
3) Another thing I noticed, if I say self.presentingViewController, EmptyViewController handle is returned while popViewController returns me back to MainViewController.
Thanks
The App Delegate simply implements some app-level 'callbacks' by which iOS communicates with your own code. In main.m you can see how iOS is told which of your classes implements UIApplicationDelegate. iOS creates an instance of this class and call these delegate methods ('callback') whenever appropriate (e.g. when the app goes to background).
The Window is something iOS provides, your app needs to tell what to display on it. And, as you saw, this is usually done in didFinishLaunchingWithOptions (which is called by iOS to inform your app things are ready to get started).
A View Controller is a class that handles states of stuff you show on the Window. You don't show stuff directly on the Window, but instead use Views. Every View Controller has a View with UI elements.
The XIB or NIB is a UI description/layout file. A XIB and View are linked together; you need to tell the XIB to which View Controller member (e.g. a UILabel) a UI element belongs, and you tell the XIB which View Controller method to call on a certain UI event (e.g. user taps on a button).
These are the basics. I'm aware it does not answer all your questions; I suggest you read the very good Apple documentation. Don't try to understand everything immediately as things, as you're experiencing, indeed can seem illogical at start.

Create UINavigationController in other xib?

Hello, I have this code in my AppDelegate:
[window addSubview:viewController.view];
[window makeKeyAndVisible];
return YES;
Now I want to create a XIB file in which I want to put a UINavigationController and add a UITableView. How do I create this through code without changing the delegate class?
I've tried this but it does not work:
PlacesTableViewController *obj = [[PlacesTableViewController alloc]init];
obj.title = #"Farmacie intorno a te";
UINavigationController *navC = [[UINavigationController alloc]initWithRootViewController:obj];
[self.window addSubview:navC.view];
First, the "modern" way to set up your window is to use the window's rootViewController property:
window.rootViewController = someViewController;
The old way, where you add a view controller's view to the window as a subview, still works but the app will log a complaint about wanting the root view controller to be set up by the time the app is done launching.
Second, if you're going to replace the root view controller (in this case you're replacing your viewController with navC) using the old style, you'd want to remove the old view controller's view from the window. Your best bet is to just use the window's rootViewController property, since your code will be expected to use that going forward anyway.

load view controller when application becomes active

I will like my application that always starts as the first time I open it. I have several view controllers and when I exit my application and I open it again I see the view controller where I left of. Maybe I have to call the method applicationWillTerminate method.
I use this code to open a new view:
UIViewController *control = [[SomeViewController alloc] initWithNibName:#"SomeViewController"
bundle:nil];
UINavigationController *navControl = [[UINavigationController alloc]
initWithRootViewController:control];
[self presentModalViewController:navControl animated:NO];
[navControl setNavigationBarHidden:YES];
[control release];
[navControl release];
this code works great when linking it to buttons. But when I place that code in the applicationDidBecomeActive method it does not work.
The easiest way is to set UIApplicationExitsOnSuspend in Info.plist.
That really isn't the expected behaviour, though. Users expect to see the app "where they left off", especially if they've only briefly left the app (e.g. because they got a phone call).
Your code snippet adds a view controller, but is unlikely to work since your app delegate is not a UIViewController. It also doesn't do anything about removing the old view controllers.
EDIT: If all you need to do is display a splash screen (or something), then it's something like this:
In -applicationDidEnterBackground:, add a "splash screen" view (not a view controller) to self.window. (iOS takes a "screenshot" after you return from -applicationDidEnterBackground: and uses this in the app-switch animation; you want this to be what the user sees when switching back to your app)
In -applicationWillEnterForeground:, do whatever animations you want and eventually remove the view from the window (call -removeFromSuperview).
EDIT 2: The same will work in -applicationWillResignActive:/-applicationWillBecomeActive:, except this happens on a sleep/wake event, which might not be what you want...
I'd avoid using view controllers for this, because trying to shoehorn a view controller in the view controller hierarchy is likely to be problematic (for example, you have to figure out which VC to present it from, and you have to do the "right thing" if the user backgrounds your app while the VC is on screen, and...)
The reason it doesn't work in applicationDidBecomeActive is that method is only sent to the Application delegate, which doesn't know about presentModalViewController.
I suggest instead that in your appDelegate, implement applicationWillEnterForeground:, which should restore the state to a newly launched application (equivalent to what the state is at the end of application:didFinishLaunchingWithOptions: ).
OR...(edits)
If you just want a certain viewController to run (which is still loaded, right?)...For example, if you have a tab controller and just want to go to the root of the first view controller, put the following code into applicationWillEnterForeground:
UITabBarController * myTabBar = self.tabBarController;
myTabBar.selectedIndex = 0;
[[myTabBar.viewControllers objectAtIndex:0] popToRootViewControllerAnimated:NO];
Temporary solution:
I made my application crash on applicationWillResignActive method and it works. My application needs to run an animation when I launch it. But this works because next time the application runs it starts like the first time I opened it.

How do I do a View Transition in an iPhone app without allowing back?

This is a total noob question.
I have a starting view -- it's very simple: just some text and a button. When the user clicks the button, I want to go to the real "meat" of the application, which is a Navigation/Table View. How do I connect the button on the IntroViewController to a transition to the RootViewController? I don't want to make the IntroViewController a full Navigation controller and push the new view because that lets the user go back. I'm looking for some combination of code snippets and Interface Builder instructions.
If you're using a UINavigationController you could just use setViewControllers:animated:
You could even fake your "back" history if you wanted to go to someplace you've never been before.
For the comments below, here's what I have in my application delegate:
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// Override point for customization after app launch
navController = [[UINavigationController alloc] initWithRootViewController: viewController];
[window addSubview:navController.view];
[window makeKeyAndVisible];
}

Iphone changing to another view [BEGINNER]

Hi I have a small doubt, I have 3 Nib Files:
ConfigureContacts.xib
CallContactsViewController.xib
MainWindow.xib
When Application starts I do:
[window addSubview:callContactsViewController.view];
[window makeKeyAndVisible];
So that the CallContactsViewController.xib is loaded.
Inside CallContactsViewController.xib there is a button, that when presses jumps to:
-(IBAction)configureContacts:(id)sender
{
configureContacts = [[ConfigureContacts alloc] initWithNibName:#"ConfigureContacts" bundle:nil];
[self.view addSubview:configureContacts.view];
}
The idea is that when the button is pressed it goes to the "next window" which is the other .xib file. Is this the correct way of changing through xibs ? Am I wasting memory here ?
Thanks in advance
is ConfigureContacts a ViewController subclass? (It looks like it is)
if so you would actually do something like this
-(IBAction)configureContacts:(id)sender
{
configureContacts = [[ConfigureContacts alloc] initWithNibName:#"ConfigureContacts" bundle:nil];
[self pushviewController:configureContacts animated:YES];
}
My Guess this is what you want to do.
[self.view addSubview:configureContacts.view] should work as well however it will change only part of the view and keep you on the same 'Window' so to speak.
pushViewController is a method from UINavigationController, which is used for managing hierarchical viewControllers. It sounds like that might be what you're trying to do here, in which case adding a UINavigationController to your code might be the way to go.
Alternatively, you could implement a third UIViewController that owns both CallContrats and ConfigureContacts and is responsible for switching between them.
Check out the sample code from Beginning iPhone Development 3. Specifically, check out the programs "06 View Switcher" and "09 Nav". I think they'll have the code that you're looking for.