If You Don't Need Interface Builder.

AID: 3714
  • Status: Published

8580 points

  • Bypgnatyuk
  • TypeTips/Tricks
  • Posted on2010-09-17 at 05:32:25
Awards
  • Community Pick
  • Experts Exchange Approved

Preface


I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small and trivial, when few standard GUI controls could be arranged easily on the basic view and, then, connected to my code objects in Interface Builder.

At some point, though, the programs became a little bigger than a simple tutorial program, a little more complicated than one full-screen view with three labels and one button. Now I have to worry about having enough memory for my application and I so I now desire to eliminate everything related to Interface Builder.

This article shows how to accomplish this using an easy and fast trick.

iPhone Application Project.


Let's make a simple iPhone project:
1

Create new iPhone window-based application project.


In Xcode, in menu File, select "New Project...". In the project wizard sidebar select Application in the iPhone OS section. Select Windows-based Application in the right panel and click on Choose button in the bottom-right corner. In the Save panel give a name for the project. For example, "Simple", and click Save button.

These two screenshots illustrate this step:

New-Project.jpg
  • 52 KB
  • Xcode Project Wizard
Xcode Project Wizard

Xcode.jpg
  • 13 KB
  • Set Project Name
Set Project Name


2

Delete Classes folder and xib-file.


In Xcode project window, find Classes folder and delete it - select "Classes" item, click on the right mouse button and in the popup menu select "Delete". In the alert window click on "Also move to trash" button.

When the project (the most top item) is selected in the Xcode sidebar, on the right panel you see all project files. Find MainWindow.xib file and delete it too.

Here are the screenshots:

Simple.jpg
  • 66 KB
  • Xcode Project Window
Xcode Project Window

Xcode-1.jpg
  • 72 KB
  • Delete Classes Group
Delete Classes Group

Xcode-2.jpg
  • 53 KB
  • Delete Alert
Delete Alert

Xcode-3.jpg
  • 86 KB
  • Delete MainWindow.xib
Delete MainWindow.xib


3

Modify *-info.plist file.


Click on Simple-info.plist file to select it. The bottom panel shows the file content. There is a line about the MainWindow.xib file:  "Main nib-file base name". The last one in my file. Delete it.

Simple-Info.plist---Simple.jpg
  • 109 KB
  • Delete Simple-info.plist
Delete Simple-info.plist


4

Modify main.m file.


Source code of this program will be in main.m file. Open this file for editing and add new application delegate class. Use the class name in the main function. Basically, it means to replace the original content of main.m file with the following code:

#import <UIKit/UIKit.h>

// Application delegate
@interface Simple : NSObject <UIApplicationDelegate>
{
}

@end

@implementation Simple

- (void)applicationDidFinishLaunching: (UIApplication*)application
{
    UIWindow* window = [[UIWindow alloc] initWithFrame: [[UIScreen mainScreen] bounds]];
    [window setBackgroundColor: [UIColor orangeColor]];
    [window makeKeyAndVisible];
}

@end

int main(int argc, char *argv[]) 
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    int retVal = UIApplicationMain(argc, argv, nil, @"Simple");
    [pool release];
    return retVal;
}
                                    
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:

Select allOpen in new window



New application delegate class defined in the code above has name Simple. This name is used in the main function.

int retVal = UIApplicationMain(argc, argv, nil, @"Simple");
                                    
1:

Select allOpen in new window


5

Run on iPhone Simulator.


Be sure that the active configuration is Debug and Simulator is selected as the target device. Popup button in the Xcode Toolbar allows changing the active configuration and the target device.

System.jpg
  • 103 KB
  • Configuration
Configuration


Press "Build & Run" button to launch the program.

App-running.jpg
  • 34 KB
  • Simple Application Running.
Simple Application Running.



Improvements


This program does not have an icon, so iPhone Simulator shows a white rectangle instead. Icon for the iPhone application is a PNG-image. In the simplest case it has size 57x57 pixels and the name is icon.png. Drag any image that fits to this requirement to the resource folder in the Xcode project. Do not forget to check "Copy item to the destination group folder":
Xcode-4.jpg
  • 33 KB
  • Project resources
Project resources

icon.png
  • 7 KB
  • Icon for iPhone project
Icon for iPhone project
Default.png
  • 136 KB
  • Image for the splash screen
Image for the splash screen

I attached my icon.png file as an example. I attached also Default.png - this is another special image that is used by iOS without any coding required from the developer. Add this image to the project resources. Build and launch the application. When it is loaded, this Default.png works as a splash screen.
Default.png---Simple.jpg
  • 87 KB
  • Default.png added
Default.png added


Practical Use


Everything related to Interface Builder was removed from the iPhone project. The source code is concentrated in main.m file. It is very convenient when I'm learning, when I need to check or to test something quickly - a language feature,  new API or an idea.

For example, the code below placed in the -applicationDidFinishLaunching method will show how to work the Cocoa date/timer formatter:
- (void)applicationDidFinishLaunching: (UIApplication*)application
{
    UIWindow* window = [[UIWindow alloc] initWithFrame: [[UIScreen mainScreen] bounds]];
    [window setBackgroundColor: [UIColor orangeColor]];

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDoesRelativeDateFormatting: YES];
    [formatter setDateStyle: NSDateFormatterLongStyle];
    [formatter setTimeStyle: NSDateFormatterShortStyle];
    
    NSDate *now = [NSDate date];
    NSString *formattedDateTime = [formatter stringFromDate: now];
    
    NSLog(@"Formatted Date: %@", formattedDateTime);
    
    [formatter release];	
    [window makeKeyAndVisible];
}
                                    
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:

Select allOpen in new window


iPhone-Simulator-3.jpg
  • 99 KB
  • Running application
Running application

Deleted xib-file does not mean that my program cannot have a GUI. Next code snippet demonstrate a GUI application:
#import <UIKit/UIKit.h>

@interface SimpleController : UIViewController
{
}
@end

@implementation SimpleController

- (id)init
{
	if (self = [super init])
	{
		self.title = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleName"];
	}
	return self;
}

- (void)loadView
{
	// Load an application image and set it as the primary view
	UIImageView* contentView = [[UIImageView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
	[contentView setImage:[UIImage imageNamed:@"Abackground.png"]];
	
	// Provide support for auto-rotation and resizing
	contentView.autoresizesSubviews = YES;
	contentView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
	
	// Assign the view to the view controller
	self.view = contentView;
        [contentView release]; 
}

-(void) dealloc
{
	[super dealloc];
}
@end

@interface Simple : NSObject <UIApplicationDelegate> 
{
}
@end

@implementation Simple

- (void)applicationDidFinishLaunching:(UIApplication *)application 
{	
	UIWindow *window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
	UINavigationController *nav = [[UINavigationController alloc] 
                                                          initWithRootViewController:[[SimpleController alloc] init]];
	[window addSubview:nav.view];
	[window makeKeyAndVisible];
}

- (void)applicationWillTerminate:(UIApplication *)application  
{
}

- (void)dealloc 
{
	[super dealloc];
}

@end

int main(int argc, char *argv[])
{
	NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
	int retVal = UIApplicationMain(argc, argv, nil, @"Simple");
	[pool release];
	return retVal;
}
                                    
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:

Select allOpen in new window


This code defines the simplest view controller (SimpleController class) which creates a UIImageView and set an image to it. Here is the application screenshot:
iPhone-Simulator-4.jpg
  • 36 KB
  • Application screenshot.
Application screenshot.
    Asked On
    2010-09-17 at 05:32:25ID3714
    Tags

    Xcode

    ,

    Interface Builder

    ,

    xib-file

    Topic

    Objective-C Programming Language

    Views
    3440

    Comments

    Add your Comment

    Please Sign up or Log in to comment on this article.

    Join Experts Exchange Today

    Gain Access to all our Tech Resources

    Get personalized answers

    Ask unlimited questions

    Access Proven Solutions

    Search 3.2 million solutions

    Read In-Depth How-To Guides

    1000+ articles, demos, & tips

    Watch Step by Step Tutorials

    Learn direct from top tech pros

    And Much More!

    Your complete tech resource

    See Plans and Pricing

    30-day free trial. Register in 60 seconds.

    Loading Advertisement...

    Top Objective-C Experts

    1. Programmer-x

      6,000

      0 points yesterday

      Profile
      Rank: Guru
    2. pgnatyuk

      5,380

      0 points yesterday

      Profile
      Rank: Genius
    3. edster9999

      1,500

      0 points yesterday

      Profile
      Rank: Sage
    4. vks_vicky

      1,000

      0 points yesterday

      Profile
      Rank: Master
    5. SpeedyApocalypse

      690

      0 points yesterday

      Profile
      Rank: Wizard
    6. vigilparikh

      250

      0 points yesterday

      Profile
      Rank: Master

    Hall Of Fame