iPhone SecondApp: Guess the number - Part 2

Monday, July 13, 2009

As 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:

newproject

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:

deleteib

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:

Objective-C:
  1. int argc, char * argv [ ] ) { int main (int argc, char * argv []) (
  2. pool = [ [ NSAutoreleasePool alloc ] init ] ; NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
  3. / / Having deleted the file. Xib we lost the point
  4. / / Delegate to the JPA, then changes to "hand"
  5. UIApplicationMain ( argc, argv, nil , @ "SecondAppAppDelegate" ) ; int retVal = UIApplicationMain (argc, argv, nil, @ "SecondAppAppDelegate");
  6. ; [Pool release];
  7. return retVal;
  8. )

Then we open SecondAppAppDelegate.me create the main window via code:

Objective-C:
  1. ) applicationDidFinishLaunching : ( UIApplication * ) application { - (Void) applicationDidFinishLaunching: (UIApplication *) application (
  2. / / Get the screen size (320, 480)
  3. applicationFrame ] ; CGRect windowRect = [[UIScreen Mainscreen] applicationFrame];
  4. / / Create a window - as we did bye bye to Interface Builder
  5. UIWindow alloc ] initWithFrame : windowRect ] ; UIWindow mainWindow * = [[UIWindow alloc] initWithFrame: windowRect];
  6. / / Set the background of the Window to yellow, to differentiate
  7. / / From the previous application FirstApp
  8. UIColor yellowColor ] ] ; [MainWindow setBackgroundColor: [UIColor yellowColor]];
  9. mainWindow ] ; [Self setWindow: mainWindow];
  10. ; [Window makeKeyAndVisible];
  11. ; [MainWindow release];
  12. )

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

Objective-C:
  1. # import <UIKit/UIKit.h>
  2. NSObject <UIApplicationDelegate> { @ interface SecondAppAppDelegate: NSObject (<UIApplicationDelegate>
  3. UIWindow * window;
  4. UITextField * number;
  5. UIButton * button;
  6. int numeroACaso;
  7. )
  8. nonatomic, retain ) UIWindow * window; @ property (nonatomic, retain) UIWindow * window;
  9. ) controllaNumero; - (Void) controllaNumero;
  10. @ 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:

Objective-C:
  1. / / Create the title bar
  2. 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)];
  3. myNavigationBar.barStyle = UIBarStyleDefault;
  4. UINavigationItem alloc ] initWithTitle : @ "Indovina un numero" ] ; UINavigationItem = navigationItem * [[UINavigationItem alloc] initWithTitle: @ "Guess a number"];
  5. navigationItem animated : NO ] ; [MyNavigationBar pushNavigationItem: navigationItem animated: NO];
  6. myNavigationBar ] ; [Window addSubview: myNavigationBar];
  7. / / Create the label
  8. UILabel alloc ] initWithFrame : CGRectMake ( 10, 50, 300, 80 ) ] ; UILabel myLabel * = [[UILabel alloc] initWithFrame: CGRectMake (10, 50, 300, 80)];
  9. ; myLabel.backgroundColor = [UIColor clearColor];
  10. ; myLabel.numberOfLines = 2;
  11. ; myLabel.text = @ "iPhone has designed a number from 1 to 10, try to guess?"
  12. myLabel ] ; [Window addSubview: myLabel];
  13. / / Create text input
  14. initWithFrame : CGRectMake ( 10, 120, 300, 30 ) ] ; number = [[UITextField alloc] initWithFrame: CGRectMake (10, 120, 300, 30)];
  15. numero.borderStyle = UITextBorderStyleRoundedRect;
  16. numero.textAlignment = UITextAlignmentCenter;
  17. numero.keyboardType = UIKeyboardTypeNumberPad;
  18. ; numero.placeholder = @ "Enter number";
  19. numero ] ; [Window addSubview: number];
  20. / / Create the button
  21. UIButtonTypeRoundedRect ] ; button = [UIButton buttonWithType: UIButtonTypeRoundedRect];
  22. 10, 180, 300, 30 ) ; bottone.frame = CGRectMake (10, 180, 300, 30);
  23. @ "Premi qui" forState : UIControlStateNormal ] ; [Button setTitle: @ "Click here" Forst: UIControlStateNormal];
  24. self action : @selector ( controllaNumero ) forControlEvents : UIControlEventTouchUpInside ] ; [Button addTarget: self action: @ selector (controllaNumero) forControlEvents: UIControlEventTouchUpInside];
  25. bottone ] ; [Window addSubview: button];
  26. ; [MyLabel release];
  27. ; [NavigationItem release];
  28. ; [MyNavigationBar release];

Since the applicationDidFinishLaunching matches viewDidLoad the last time, just after the [mainWindow release]; entered:

Objective-C:
  1. 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:

Objective-C:
  1. ) controllaNumero { - (Void) (controllaNumero
  2. ) ; NSLog (@ "push-button numerical control");
  3. numero.text integerValue ] ; int numeroInserito = [numero.text integerValue];
  4. message; NSString * message;
  5. , numeroInserito ) ; NSLog (@ "The number entered is% d", numeroInserito);
  6. numeroInserito <numeroACaso ) { if (numeroInserito <numeroACaso) (
  7. ; @ message = "Too low ..."
  8. ( numeroInserito> numeroACaso ) { ) Else if (numeroInserito> numeroACaso) (
  9. ; @ message = "Too high ..."
  10. ( numeroInserito == numeroACaso ) { ) Else if (numeroInserito == numeroACaso) (
  11. ; message = @ "Bravo, you guessed it;
  12. arc4random ( ) % 10 ; numeroACaso = 1 + arc4random ()% 10;
  13. , numeroACaso ) ; NSLog (@ "Number% d thought," numeroACaso);
  14. )
  15. UIAlertView alloc ] UIAlertView * alertMessaggio = [[UIAlertView alloc]
  16. initWithTitle: @ "Response"
  17. Message: message
  18. delegate: nil
  19. cancelButtonTitle: @ "OK"
  20. ] ; otherButtonTitles: nil];
  21. ; [AlertMessaggio show];
  22. ; [AlertMessaggio release];
  23. ; numero.text @ = "";
  24. )

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.

Related Post

Was this article helpful?: Per nientePocoAbbastanzaMoltoMoltissimo
Loading ... Loading ...

There are no comments for this post

Leave a comment

TAG XHTML PERMISSIONS: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> INSERTION CODE:
 <pre></pre>         // blocco generico [code][/code]       // blocco generico [as][/as]           // Actionscript [css][/css]         // CSS Style Sheet [html][/html]       // HTML [js][/js]           // Javascript [objc][/objc]       // Objective-C [php][/php]         // PHP [sql][/sql]         // SQL