iPhone SecondApp: Guess the number - Part 2
Monday, July 13, 2009As anticipated in iPhone FirstApp: Guess the number - Part 1 we see how to make an application for Apple iPhone without using Interface Builder! Indeed, physically delete files created by Interface Builder wizard Xcode. At the end of this post, then we will have an application identical in all respects to that made in Part One, with the difference that we achieve all our visual components, including the main window, completely to code.
The application already made, if you only want to download, is available on my Google Code repository:
I should point out immediately as the ZIP code of this sample weights less than last time! ![]()
Create the project
We start creating our project SecondApp (to distinguish it from FirstApp), although this time choose Window-based Application:

Now let's remove everything related to Interface Builder. Delete the file MainWindow.xib located in the folder Resources even delete it from the file system, then select Also Move to Trash. By clicking on the file SecondoApp-info.plist and delete the reference to MainWindow Section Main nib file base name:

At this point we have no Window, at least through Interface Builder. Then open the file main.m located in Other Sources and modify the function main() like so:
- int argc, char * argv [ ] ) { int main (int argc, char * argv []) (
- pool = [ [ NSAutoreleasePool alloc ] init ] ; NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
- / / Having deleted the file. Xib we lost the point
- / / Delegate to the JPA, then changes to "hand"
- UIApplicationMain ( argc, argv, nil , @ "SecondAppAppDelegate" ) ; int retVal = UIApplicationMain (argc, argv, nil, @ "SecondAppAppDelegate");
- ; [Pool release];
- return retVal;
- )
Then we open SecondAppAppDelegate.me create the main window via code:
- ) applicationDidFinishLaunching : ( UIApplication * ) application { - (Void) applicationDidFinishLaunching: (UIApplication *) application (
- / / Get the screen size (320, 480)
- applicationFrame ] ; CGRect windowRect = [[UIScreen Mainscreen] applicationFrame];
- / / Create a window - as we did bye bye to Interface Builder
- UIWindow alloc ] initWithFrame : windowRect ] ; UIWindow mainWindow * = [[UIWindow alloc] initWithFrame: windowRect];
- / / Set the background of the Window to yellow, to differentiate
- / / From the previous application FirstApp
- UIColor yellowColor ] ] ; [MainWindow setBackgroundColor: [UIColor yellowColor]];
- mainWindow ] ; [Self setWindow: mainWindow];
- ; [Window makeKeyAndVisible];
- ; [MainWindow release];
- )
You can already test the application, if there is a yellow window you did everything right!
In the file SecondAppAppDelegate.h we can eliminate IBOutlet necessary only if you use Interface Builder. Also add here our global variables that, last time we had entered in the Controller. Then edit the file SecondAppAppDelegate.h
- # import <UIKit/UIKit.h>
- UIWindow * window;
- UITextField * number;
- UIButton * button;
- int numeroACaso;
- )
- nonatomic, retain ) UIWindow * window; @ property (nonatomic, retain) UIWindow * window;
- ) controllaNumero; - (Void) controllaNumero;
- @ end
Again we have prepared the definition of the method controllaNumero like last time, but we have removed the indication IBAction because we are not using Interface Builder.
We build the interface from code
It 'time to create code using all components of our interface. We return to the file SecondAppAppDelegate.m posizioniamoci before [mainWindow release] and insert the following code:
- / / Create the title bar
- UINavigationBar alloc ] initWithFrame : CGRectMake ( 0.0, 0.0, 320.0, 44.0 ) ] ; UINavigationBar * myNavigationBar = [[UINavigationBar alloc] initWithFrame: CGRectMake (0.0, 0.0, 320.0, 44.0)];
- myNavigationBar.barStyle = UIBarStyleDefault;
- UINavigationItem alloc ] initWithTitle : @ "Indovina un numero" ] ; UINavigationItem = navigationItem * [[UINavigationItem alloc] initWithTitle: @ "Guess a number"];
- navigationItem animated : NO ] ; [MyNavigationBar pushNavigationItem: navigationItem animated: NO];
- myNavigationBar ] ; [Window addSubview: myNavigationBar];
- / / Create the label
- UILabel alloc ] initWithFrame : CGRectMake ( 10, 50, 300, 80 ) ] ; UILabel myLabel * = [[UILabel alloc] initWithFrame: CGRectMake (10, 50, 300, 80)];
- ; myLabel.backgroundColor = [UIColor clearColor];
- ; myLabel.numberOfLines = 2;
- ; myLabel.text = @ "iPhone has designed a number from 1 to 10, try to guess?"
- myLabel ] ; [Window addSubview: myLabel];
- / / Create text input
- initWithFrame : CGRectMake ( 10, 120, 300, 30 ) ] ; number = [[UITextField alloc] initWithFrame: CGRectMake (10, 120, 300, 30)];
- numero.borderStyle = UITextBorderStyleRoundedRect;
- numero.textAlignment = UITextAlignmentCenter;
- numero.keyboardType = UIKeyboardTypeNumberPad;
- ; numero.placeholder = @ "Enter number";
- numero ] ; [Window addSubview: number];
- / / Create the button
- UIButtonTypeRoundedRect ] ; button = [UIButton buttonWithType: UIButtonTypeRoundedRect];
- 10, 180, 300, 30 ) ; bottone.frame = CGRectMake (10, 180, 300, 30);
- @ "Premi qui" forState : UIControlStateNormal ] ; [Button setTitle: @ "Click here" Forst: UIControlStateNormal];
- self action : @selector ( controllaNumero ) forControlEvents : UIControlEventTouchUpInside ] ; [Button addTarget: self action: @ selector (controllaNumero) forControlEvents: UIControlEventTouchUpInside];
- bottone ] ; [Window addSubview: button];
- ; [MyLabel release];
- ; [NavigationItem release];
- ; [MyNavigationBar release];
Since the applicationDidFinishLaunching matches viewDidLoad the last time, just after the [mainWindow release]; entered:
- arc4random ( ) % 10 ; numeroACaso = 1 + arc4random ()% 10;
Now we can do is implement the method controllaNumero which will be identical (apart from the prototype) to the one used last time:
- ) controllaNumero { - (Void) (controllaNumero
- ) ; NSLog (@ "push-button numerical control");
- numero.text integerValue ] ; int numeroInserito = [numero.text integerValue];
- , numeroInserito ) ; NSLog (@ "The number entered is% d", numeroInserito);
- numeroInserito <numeroACaso ) { if (numeroInserito <numeroACaso) (
- ; @ message = "Too low ..."
- ( numeroInserito> numeroACaso ) { ) Else if (numeroInserito> numeroACaso) (
- ; @ message = "Too high ..."
- ( numeroInserito == numeroACaso ) { ) Else if (numeroInserito == numeroACaso) (
- ; message = @ "Bravo, you guessed it;
- arc4random ( ) % 10 ; numeroACaso = 1 + arc4random ()% 10;
- , numeroACaso ) ; NSLog (@ "Number% d thought," numeroACaso);
- )
- UIAlertView alloc ] UIAlertView * alertMessaggio = [[UIAlertView alloc]
- initWithTitle: @ "Response"
- Message: message
- delegate: nil
- cancelButtonTitle: @ "OK"
- ] ; otherButtonTitles: nil];
- ; [AlertMessaggio show];
- ; [AlertMessaggio release];
- ; numero.text @ = "";
- )
We're done!
Conclusions and considerations
This example does not make direct use of a UIView or a UIViewController just because I wanted to leave it as simple and streamlined as possible and, also, to show that there are elements still needed. However for objects directly in the window may have some sense in this example and in other sporadic contexts. The use of UIView and UIViewController still bring benefits in many other cases, andin some are practically indispensable, as we shall see in the future.














There are no comments for this post
Leave a comment