Understanding Outlets in Interface Builder - iphone

I'm doing an exercise to try to truly understand Interface Builder and Outlets.
Using Xcode 4.0 I've chosen the template for Window Based Application. I then:
Go into Interface Builder and add a UINavigation Controller.
Set the Window Outlet RootView Controller to the Navigation Controller.
Go to AppDelegate.h to create a UINavigation Controller called
navController and give it an IBOutlet.
Go to AppDelegate.m in the didFinishLaunchingWithOptions to set
self.window.rootViewController to self.navController.
Create a new UIViewController called FirstViewController complete
with .xib file.
My question is, how do I set the root view controller in the mainWindow.xib outlet as the FirstViewController? But more importantly could someone explain to me the reasoning behind how you do this?
In other words, I would like FirstViewController to be the first view controller the user sees in my app. And I would like to understand exactly the mechanics behind making this happen.
Thanks for all the help!

interface bilder's outlets is just a links between graphical part of your program, and your code. In other words, they explane what code controlls this graphical object. You see, objective-c is very MVC-oriented. .xib file is V(view), outlet and your code, that linked with .xib by this outlet - is C(controller). M(model) should be somewhere alse in your code.
Now, about root view controller. I prefer implement it like below:
import your firstViewController to AppDeledate.h and implement navigationController:
#import "firstViewController .h"
#class firstViewController
...
UINavigationController *navigationController;
...
#property (nonatomic, retain) IBOutlet UINavigationController *navigationController;
now, in interfacebuilder (mainWindow.xib), add navigationController object and link it with implemented property. Then, add a ViewController object in that navigationController, select this ViewController Object and set in inspectors it's class to firstViewController and xib file to firstViewController. Finaly, in AppDelegate.m in the didFinishLaunchingWithOptions to set self.window.rootViewController to self.navigationController. That's it.

Related

hooking up two different storyboard viewcontrollers to a same class

I am new to Iphone development. My app consists of two storyboards one for the Ipad and other for the Iphone.Now, my problem is I have an IBOutletCollection of UILabels hooked up as a property with one of the viewcontrollers in my Iphone storyboard..How am I suppose to hook up the same Ipad storyboard viewcontroller with the IBOutletCollection of UILabels for the same class..?
Thanks in advance..
Just like you'd hook up any other IBOutlet, IBOutletCollection or IBAction.
Your UIViewController sub-class contains the bits which let you hook something in a storyboard to it.
#property (strong, nonatomic) IBOutletCollection(UIButton) NSArray *tabButtons;
In your storyboard, you can assign the view controller to be of a certain class, in this case it will be a your UIViewController sub-class. Now all you have to do it drag the storyboard artifacts to your existing IBOutlet stubs. It works.
You can do this for multiple storyboards using the same class (or multiple view controller in the same storyboard too). When you load it into memory, you specify the item in the storyboard, then the storyboard creates and instance of the view controller and hooks up all the references, so all is good.
MyViewController *myVC = [storyboard instantiateViewControllerWithIdentifier:#"foo"];

how to add a tab Bar to a Default view and communicate with other views using navigation controller

It might be simple but not able to find exact solution for this.i am using xcode 4.2.
I want to use Tab bar in one of the view in my application. i went through many tutorials all tutorials are related to navigation based application and other view based application.Even i understood how to add a tab bar controller in story board which is main view.
What i need is i have class called Homepage.h and .m and .xib which is subclass of UIViewController class.Again my class is not main class its added later for one of the view.So i want to add a tab bar and communicate with navigationBar and other views so how can i do it plz give me some samples.
Problem is i want to add Tab bar to the default UIView and communicate with navigation controller and other views. i dont want to drag Tab bar controller from utilities.Incase if i drag how can i make it view on moving from one view to another as i already have a Default UIview.Please give me links or any tutorials where i can add tab bar and switch between views using navigation controller.
NOTE: i am using Single View based Application
Take UINavigationController object and UITabBarController object in AppDelegate.h
In AppDelegate.h
First *first;
Second *second;
Third *third;
UINavigationController *navController;
UITabBarController *tabbar;
#property (nonatomic, retain) UITabBarController *tabbar;
#property (nonatomic, retain) UINavigationController *navController;
IN AppDelegate.m
#synthesize tabbar,navController;
in ApplicationdidFinishLaunching
tabbar=[[UITabBarController alloc]init];
first=[[First alloc]initWithNibName:#"First" bundle:nil];
second=[[Second alloc]initWithNibName:#"Second" bundle:nil];
third=[[Third alloc]initWithNibName:#"Third" bundle:nil];
navController=[[UINavigationController alloc]initWithRootViewController:first];
NSArray *viewControllerArray=[[[NSArray alloc] initWithObjects:navController1,second,third,nil] autorelease];
[self.window addSubview:tabbar.view];
[tabbar setViewControllers:viewControllerArray];
[first setTitle:#"First"];
[second setTitle:#"Second"];
[third setTitle:#"Third"];
Write this code and dont need to put Tabbar in XIB. Try thi code it will helps you.

about navigation controller in view controller

In the appdelagate, we have a UINavigationController and the view controllers as well. then in it we can initialize the navigation controller with the root view controller. And I understand why need them too.
However, in the sample code of my reference book(iPhone SDK Application Development, author: Jonathan Zdziarski), all view controller classes were added with a navigation controller as property, while they seem to be never used. So what is the meaning of having them as property in view controller classes?
e.g
#interface XYZViewController: UIViewController
{
UITextView *textView;
UIButton *button;
.....
.....
UINavigationController *navigationController;
}
-(void)...
.....
...
#end
One more question:
All UIViewController instances can have the property "navigationItem" once they are navigated. so what do this navigationItem refer to? does it refer to the navigation controller that is navigating the view controller?
UIViewController has navigationController property that is handled by the framework and it points to the parent UINavigationController if the view controller in question has one.
Check the documentation for more info:
http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UIViewController_Class/Reference/Reference.html
The same documentation will tell you that navigationItem is a UINavigationItem object that represents a view controller in the navigation bar. You can customise its appearance, like title, prompt, back button behaviour, etc.
That said, I have no idea why your book adds a navigationController property to UIViewController subclasses. It was added in iOS 2.0, a long time ago already... Anyway, you shouldn't need to add it, as it's provided in UIViewController class.

iPhone Dev - Where is RootViewController instantiated in a Navigation-based application?

I can't seem to find where it's actually instantiated. I looked in the myProjAppDelegate.m and saw this:
self.window.rootViewController = self.navigationController;
But it says that the window's rootViewController property is really just a UIViewController, not a UITableViewController, which is what the RootViewController.m class is a subclass of. I wrote a custom method in my RootViewController.m and tried to call it on self.navigationController in myProjAppDelegate.m and got a SIGABRT, so it seems like this is not it. Can anyone help me out?
It's not visible in code. Your MainWindow.xib contains a Window and a Navigation Controller which are connected via outlets to your AppDelegate.
Both the Window and the Navigation Controller get instantiated when the application loads the .xib files.
Inside the Window (in the MainWindow.xib) is a RootViewController, that is the RootViewController you are talking about.
Regarding the class, UITableViewController inherits from UIViewController.

Everything is wrong when I modified standard UITabBar application

I created "New Project" -> Tab Bar Application.
Then i changed from #interface FirstViewController : UIViewController to #interface FirstViewController : UINavigationController.
Then i changed file's owner from UIViewController to UINavigationController in xib file.
Then i updated view. But i don't see any label on the screen. Why? (i have some labels on xib)
Why are you subclassing UINavigationController and what are you trying to accomplish? UINavigationController does not display a view of it's own, just the navigation bar over some other view controller's view. In addition UINavigationController was not designed to be subclasses, hence the "This class is not intended for subclassing." warning in its class reference.
If you want to display your view controller as part of a navigation stack you should create an instance of UINavigationController, set that navigation controller as the view controller for one of your tabs, and then push an instance of your FirstViewController onto the UINavigationController.