Removing default window nib in a window based application

When creating a window based application in Xcode4, it usually create an unwanted nib file that represents the default clear white window.

Note: your class names will probably be different then mine, try to keep up, its not that hard.

Some times we want to remove it and replace it with are own view controller, here is how:

  1. delete the default MainWindow.nib. You can totally delete it, assuming you have no information in it.
  2. Click on the project (above the files – blue strip) and select the target. on the right, go to info, locate the “main nib file base name” line and remove it entirely. You can run your application now with no errors (and no output…).
  3. I assume that you have created a view controller. if not create it now. Go to your app delegate file header and import the header for your master view.
  4. declare a property for that controller and synthesize it on the m file.
  5. now we need to create the default window: self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; add this to the application didFinishLaunchingWithOptions function. BTW – the reason that we didn’t get an exception so far is because once the main nib is deleted, the entire application function will not be called.
  6. next, create the nib file: mainController=[[SwitchViewController alloc]initWithNibName:@”MainView” bundle:nil];
  7. then, assign the master controller to the window: [self.window setRootViewController:mainController];
  8. Now, the application is not even aware at this point of its delegate. This is bacause we delete the main nib file, so changes must also be done to the main.m file, hiding sometimes in the supporting files folder. this is actually very simple – just feed the UIApplicationMain function the delegate it deserves! So, instead of the default UIApplicationMain(argc, argv, nil, nil); replace the last nil with your class delegate name, like so: UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));

Now it’s working

NSStringFromClass gives a string… from the class. which is what this function requires, in case there is no nib file that loads that data for us.

 

Hiding the keyboard on iOS

To hide the keyboard on iOS devices, you need to do two simple things:

  1. Create a method like this: -(IBAction) onTextDone:(id)sender in both header and implementation files (you don’t have to in the header, but it is a good practice) . The signature is what matters here, not the actual name.
  2.  The method implementation will only contain one line: [sender resignFirstResponder]; this tells the sender to quit, in out case, the keyboard.
  3. Now, we need to inform the control about this method: click the control go to the connections inspector and control drag from the Did end on  Exit event to the file’s owner and select the method you just declared.

next time you click on the done key, the keyboard will disappear.

Adding Xib file to already existed ViewController class

Sometimes you have a UIViewController class that for some reason or another, doesn’t have a xib file attached.

Nib file, nib file, xib and Xib files are all the same files. In the new Xcode 4 and further, the old nib is now xib extension.

The reason to create an attachment to the Xib file is simple – you want to have events from controls on the GUI friendly Xib file sent to your UIVIewController object.

To add a new Xib file, select new file on Xcode and then go to user interface and select View.

 

Next, you select the family of that nib and the name, and you got a new nib.

This nib however is not connected to anything, so lets create the connection.

If you don’t already have a UIViewController class that is nib-less, create one.

Attaching the new nib to the class is very simple:

    1. Click the nib file, you will get the designer, click on the white empty view.
    2. Click on the File’s owner (the empty cube at the top)
    3. Next, go to identity inspector and from the classes select your nib class.
    4. Ctrl+Drag from the file owner box to the view (the empty white square beneath the brown square with the number “1” on it) and click on the “view” in the black menu.

That’s it! you are all done. now you can add components to this view and use them from the class by dragging them.

If you are using this UIViewController dynamically, use the initWithNibName initializer, and put your nib file name.

About iOS and protocols

iOS has something that called protocols, that has some resemblance to a Java interfaces.

  • Protocols have only functions
  • no members
  • all public (well this is obvious as objective C has no access modifiers)

Declaring a protocol:

@protocol MainControllerProtocol <NSObject>
@required
-(void)goToViewByNumber:(int)viewNumber;

@end

the @required means that this method has to be implemented in the underlining class.

Assigning protocol:

Unlike Java, assigning protocol is slightly different, as Objective C must get an actual class. BUT, we don’t have one, otherwise there is no point in the protocol! so what to do?

Here is the answer: derive a bogus class from id, like this:

id<MainControllerProtocol> controller;

This will work great. in a function, don’t forget to put this in a brackets.