Thursday 8 November 2012

How to Create and Add .xib File to Delegate


In the XCode 4.2 beta, MainWindow.xib is no longer included by default in some project templates. This means that you have to get your application to work by writing some code, using another method, or by manually reconstructing MainWindow.xib. This post shows the latter. Let’s get started.
Start with Empty Application template
If you create a new project in XCode 4.2 beta, and choose the Empty Application template to start from, change nothing and try running it in your iPhone 5.0 simulator, you will see an empty – black – screen. The only thing you get from the template is an xAppDelegate.h and .m.
We will now reconstruct our own MainWindow.xib, to get started with development the way you’re used to. So the next thing we do is add a New File to the project. Choose iOS > User Interface > Empty as template.Add Empty Interface Builder documentNext, choose iPhone, next give it the name MainWindow (.xib will be added automatically). By the way, the name of the file is not very important, but it’s nice to choose MainWindow, because that’s familiar.
Select the new File we just created. What we have now is an empty design surface, in what used to be Interface Builder. Here we’re going to change some things.
The empty design surface of MainWindow.xib
  • Change the class of File’s Owner to UIApplication
Change class of File's Owner to UIApplication
  • Find Object in the Library and drag it onto the Objects pane on the left.
Add Object to the document
  • Change the class of that Object to the xAppDelegate class that was created by the template, you might also want to clear out the “Object” label.
Change class of the object to xAppDelegate
  • Add a Window to the Objects pane on the left.
Add a window to the document
Now, let’s bind it all together. To do this, we first need to change some of the code in the xAppDelegate.h. We have to add IBOutlet to the window property it has, so that we can  hook it up in Interface Builder. The xAppDelegate.h should read something like this:
@interface DemoAppDelegate :
      UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) IBOutlet UIWindow *window;
@end
Don’t forget to save the file, otherwise Interface Builder will not be able to pick up the Outlet. Next we continue editing the MainWindow.xib
  • Control-Drag from the delegate outlet of the File Owner to the xAppDelegate object.
Link the application delegate
  • Control-Drag from the window outlet of the xAppDelegate to the Window.
Link the window outlet of the app delegate
  • Just for this demo, I’m adding a label to the window.
Add a label for testing
We’re not done yet, but we’re almost there.
  • Navigate to the project, and in the Summary tab, select MainWindow as the Main Interface.
Set the Main Interface to MainWindow
You can now run the project in the Simulator, and the window should show up. However there’s one last thing you might want to clean up. In xAppDelegate.m, there was actually code that creates a window as well. Just put the method
- (BOOL) application:didFinishLaunchingWithOptions:
in comment.
we're done


I hope this helps to understand exactly how an iOS app starts. The next thing you should do is add a ViewController, and push it onto the MainWindow. I’m not going to cover that here. Please leave your feedback in the comments.



Try This IF Not Get Result


XIB based initial View with Xcode 4.3+

Upgrading from from Xcode 4.2 to 4.3 results in the loss of the xib based Templates. This example shows how to get a the usual mainWindow.xib with a empty window (template-1) and empty window with initial view (template-2).

Template-1

1 Create a empty iPhone Project (Empty Application template)

This creates a App Delegate with minimal support files.

2 Create the mainWindow.xib (New -> User Interface -> Window)


This creates a XIB File. We also need to add a Window and AppDelegate. Add a window object and set this properties:

3 Set 'Files Owner' to UIApplication class 



4 Add an Object to 'Objects 
Change the Object Class to AppDelegate. The AppDelegate now needs a Window Property.

// AppDelegate.h
UIWindow *window;

   
@property (nonatomic,retain) IBOutlet UIWindow *window;

//AppDelgate.m
@synthesize window ;

5 Connect the AppDelegate window Outlet to the Window


change application didFinishLaunchingWithOptions
//self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];

At this point the app should show a empty white window with a error message about a missing rootview Controller.

Template-2

To get a first view from a xib we now create a Viewcontroller with xib (new File).
  1. The class of 'Files owner' should be your ViewControllers Class.
  2. Create a new view for the Viewcontrollers xib.
  3. The view is connected to 'Files owner' (ViewController view).
  4. Connect Files Owner (Controller Class) view with the view.



In application didFinishLaunching add the ViewController as rootView Controller

self.window.rootViewController = [[HBViewController1 alloc] initWithNibName:@"HBViewController1" bundle:nil];
           // for Version 3 a test is needed (respondsTo setter) and the view is added with addSubView


No comments:

Post a Comment