Click here to Skip to main content
11,647,170 members (70,169 online)
Click here to Skip to main content

Tagged as

A purist hello world app: an IPhone project template without .XIB

, 13 Aug 2010 CPOL 8.2K 1
Rate this:
Please Sign up or sign in to vote.
A step-by-step Guide to getting a pure code application template.

I have gone through quite a few hello world tutorials for the iPhone, and they always reach the point of saying ' and now some magic occurs. You don't need to worry about this... the .XIB takes care of it '

I don't like this. what if I have a project where I am not going to be using IB? it don't want a large invisible object doing unknown things to my code. that makes me feel uneasy.

So, here is a step-by-step Guide to getting a pure code application template. I am using Xcode 3 -- it should be pretty similar for other versions.

  1. create new project -> view based application "NoXIB"
  2. delete both .xib files
  3. open Info.plist, locate the key: "Main nib file base name:" and delete "MainWindow"
  4. fiddle main.m
  5. //
    //  main.m
    //  noXIB
    //
    //  Created by pi on 13/08/2010.
    //
    #import <UIKit/UIKit.h>
    int main(int argc, char *argv[]) {
        
        NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
        int retVal = UIApplicationMain(argc, argv, nil, @"noXIBAppDelegate");
        [pool release];
        return retVal;
    }
  6. can remove the IBOutlet from here:
  7. //
    //  noXIBAppDelegate.h
    //  noXIB
    //
    //  Created by pi on 13/08/2010.
    //
    #import <UIKit/UIKit.h>
    @class noXIBViewController;
    @interface noXIBAppDelegate : NSObject <UIApplicationDelegate> {
        UIWindow *window;
        noXIBViewController *viewController;
    }
    @property (nonatomic, retain) /*IBOutlet*/ UIWindow *window;
    @property (nonatomic, retain) /*IBOutlet*/ noXIBViewController *viewController;
    @end
  8. create the window and the view controller here:
  9. //
    //  noXIBAppDelegate.m
    //  noXIB
    //
    //  Created by pi on 13/08/2010.
    //
    #import "noXIBAppDelegate.h"
    #import "noXIBViewController.h"
    @implementation noXIBAppDelegate
    @synthesize window;
    @synthesize viewController;
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
    {        
        self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];;
        self.viewController = [[[noXIBViewController alloc] init] autorelease];
        // Override point for customization after app launch    
        [window addSubview:viewController.view];
        [window makeKeyAndVisible];
        
        return YES;
    }
  10. create a view; groups and files pane, right click on NoXIB -> 'classes', add new file, "MyView".
    • make sure it is a subclass of UIView.
    • override its drawRect so that it draws a red rectangle:
    //
    //  MyView.m
    //  noXIB
    //
    //  Created by pi on 13/08/2010.
    //
    #import "MyView.h"
    @implementation MyView
    :
    // Only override drawRect: if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    - (void)drawRect:(CGRect)rect {
        // Drawing code
        // Get the graphics context and clear it
        CGContextRef ctx = UIGraphicsGetCurrentContext();
        CGContextClearRect(ctx, rect);
        
        //  draw a red rectangle
        CGContextSetRGBFillColor(ctx, 255, 0, 0, 1);
        CGContextFillRect(ctx, CGRectMake(10, 10, 50, 50));
    }
  11. make the view controller load this view
  12. //
    //  noXIBViewController.m
    //  noXIB
    //
    //  Created by pi on 13/08/2010.
    //
    #import "noXIBViewController.h"
    #import "MyView.h"
    @implementation noXIBViewController
    // Implement loadView to create a view hierarchy programmatically, without using a nib.
    - (void)loadView {
        [super loadView];
        CGRect frame = CGRectMake(10, 10, 300, 300); // 320 x 460
        
        MyView* myV = [ [ [MyView alloc] initWithFrame:frame] autorelease];
        
        myV.clipsToBounds = YES;
        [[self view] addSubview: myV];
    }
    :
    :

Bingo! No XIB!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Ohmu
Architect
United Kingdom United Kingdom
I like to create and invent things. Recent projects include: A novel computer interface (Google for JediPad), a new musical system (www.toneme.org), a speech typer (http://spascii.wikispaces.com/)

Currently I am making some innovative musical instruments for iPhone/iPad

PS Currently stranded in Thailand with no money! Great! So, if you like my articles, please consider putting some coins in the box. If I have to sell my MacBook and teach in a school, that means No More Articles! PayPal is sunfish7&gmail!com. Steve Jobbs -- if you're reading this, pay me to fix your documentation.

You may also be interested in...

Comments and Discussions

 
GeneralReason for my vote of 5 Just what I needed. A well written c... Pin
Navi3-Mar-11 19:44
memberNavi3-Mar-11 19:44 
Reason for my vote of 5
Just what I needed. A well written consise article on the issue.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150804.2 | Last Updated 13 Aug 2010
Article Copyright 2010 by Ohmu
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid