Community Pick: Many members of our community have endorsed this article.
Editor's Choice: This article has been selected by our editors as an exceptional contribution.

If You Don't Need Interface Builder.

Published:

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: Xcode Project Wizard 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: Xcode Project Window Delete Classes Group Delete Alert 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. 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;
                      }

Open 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");

Open 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. Configuration
Press "Build & Run" button to launch the program.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":Project resourcesIcon for iPhone project 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 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];
                      }

Open in new window

Running applicationDeleted 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;
                      }

Open 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:
Application screenshot.

References

iOS Reference Library. The Core Application Design
2
9,562 Views

Comments (0)

Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.