Xcode 4 allows you to develop applications for iPhone or iPad with lower target today's iPhone OS 4.3. However the same code that comes with templates "sin" of arrogance, assuming that our application will target iPhone OS 4 or higher. In the application delegate, for example, Xcode inserts the following lines of code:
1 2 3 4 5 6 7
| BOOL ) application : ( UIApplication * ) application didFinishLaunchingWithOptions : ( NSDictionary * ) launchOptions - (BOOL) application: (UIApplication *) application didFinishLaunchingWithOptions: ( NSDictionary *) launchOptions { / / Override point for customization after application launch. self.window.rootViewController = self.viewController; ; [Self.window makeKeyAndVisible]; ; return YES; } |
More ...
The object UIWebView component is a very useful and versatile, ideal for solving many problems. Not only is perfect to display an HTML page on the net, but you can use it for other useful purposes such as:
- Quickly display PDF documents
- View videos or pictures
- Display content HTML / CSS Local
- Fake content in the prototype phase ...
More ...
1. Strings on multiple lines
In Xcode, you can "break" a string across multiple lines by inserting at the end with a backslash "\". This feature can be useful when, for example, we want to insert the HTML text in a control UIWebView :
More ...
As mentioned FirstApp iPhone: Guess the number - Part 1 we see how to make an application for the Apple iPhone without using Interface Builder! Indeed, we will eliminate physical files created by Interface Builder wizard Xcode. At the end of this post, then, we have an application identical in all respects, to those built in the first part, with the difference that we will achieve all our visual components, including the Main Window, for complete code.
The application already made, if you just want to download, is available on my Google Code repository:
I would like to point out immediately as the ZIP of this example weights less than the last time! 
We create the project
We start by creating our project SecondApp (to distinguish it from FirstApp), although this time choose Window-based Application:

Now let's delete everything related to Interface Builder. : eliminatelo anche dal file system, quindi selezionate Also Move to Trash . Eliminate the file MainWindow.xib , located in the folder Resources , remove it from the file system also, and then select Also Move to Trash. nella casella Main nib file base name : Select then the file SecondoApp-info.plist and delete the reference to the MainWindow nib file in the Main box based name:

At this point we no longer have any Window, at least through Interface Builder. , e modifichiamo la funzione main() in questo modo: So we open the file main.m , situated in Other Sources , and modify the function main() like so:
1 2 3 4 5 6 7 8 9 10 11
| int argc, char * argv [ ] ) { int main (int argc, char * argv []) { pool = [ [ NSAutoreleasePool alloc ] init ] ; NSAutoreleasePool * pool = [[ NSAutoreleasePool alloc] init]; / / By removing the file. XIB we lost tracking / / The app delegate and then pass it to "hand" UIApplicationMain ( argc, argv, nil , @ "SecondAppAppDelegate" ) ; int retVal = UIApplicationMain (argc, argv, nil, @ "SecondAppAppDelegate"); ; [Pool release]; return retVal; } |
Open SecondAppAppDelegate.me then we create the main Window via code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| void ) applicationDidFinishLaunching : ( UIApplication * ) application { - (Void) applicationDidFinishLaunching: (UIApplication *) application {
/ / Get the screen size (320, 480) UIScreen mainScreen ] 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 / / 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 a window appears yellow you did everything right!
, necessario solo se si usa Interface Builder. In the file SecondAppAppDelegate.h we can eliminate IBOutlet , only needed if you use Interface Builder. Also add here that our global variables, the last time we had entered in the Controller. Then modify the file SecondAppAppDelegate.h :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| # Import <UIKit/UIKit.h> NSObject <UIApplicationDelegate> { @ Interface SecondAppAppDelegate: NSObject {<UIApplicationDelegate> UIWindow * window; UITextField * number; UIButton * button; int numeroACaso; } nonatomic, retain ) UIWindow * window; @ Property (nonatomic, retain) UIWindow * window; void ) controllaNumero; - (Void) getNumber; @ End |
, in quanto non stiamo usando Interface Builder. Also in this case we have prepared the method definition controllaNumero , as the last time, but we have eliminated the indication IBAction , since we are not using Interface Builder.
We build the interface code
It 's time to create code using all components of our interface. ed inseriamo il seguente codice: We return in the file SecondAppAppDelegate.m , posizioniamoci before [mainWindow release] and insert the following code:
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
| / / 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)]; UIColor clearColor ] ; myLabel.backgroundColor = [UIColor clearColor]; ; myLabel.numberOfLines = 2; "iPhone ha pensato un numero da 1 a 10, prova ad indovinarlo?" ; myLabel.text = @ "iPhone has designed a number from 1 to 10, try to guess?" myLabel ] ; [Window addSubview: myLabel];
/ / Create text input UITextField alloc ] initWithFrame : CGRectMake ( 10 , 120 , 300 , 30 ) ] ; number = [[UITextField alloc] initWithFrame: CGRectMake (10, 120, 300, 30)]; numero.borderStyle = UITextBorderStyleRoundedRect; numero.textAlignment = UITextAlignmentCenter; numero.keyboardType = UIKeyboardTypeNumberPad; "Inserisci il numero" ; numero.placeholder = @ "Enter number"; numero ] ; [Window addSubview: number];
/ / Create the button UIButton buttonWithType : UIButtonTypeRoundedRect ] ; button = [UIButton buttonWithType: UIButtonTypeRoundedRect]; 10 , 180 , 300 , 30 ) ; bottone.frame = CGRectMake (10, 180, 300, 30); @ "Premi qui" forState : UIControlStateNormal ] ; [Button setTitle: @ "Click here" forState: UIControlStateNormal]; self action : @selector ( controllaNumero ) forControlEvents : UIControlEventTouchUpInside ] ; [AddTarget button: self action: @ selector (getNumber) forControlEvents: UIControlEventTouchUpInside]; bottone ] ; [Window addSubview: button];
; [MyLabel release]; ; [NavigationItem release]; ; [MyNavigationBar release]; |
della scorsa volta, subito dopo il [mainWindow release]; inseriamo: Since the ' applicationDidFinishLaunching corresponds to viewDidLoad as last time, immediately after the [mainWindow release]; insert:
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| void ) controllaNumero { - (Void) {getNumber "Premuto bottone di controlla numero" ) ; NSLog (@ "Pressed button control number"); [ numero.text integerValue ] ; numeroInserito int = [numero.text integerValue]; message; NSString * message; "Il numero inserito è %d" , numeroInserito ) ; NSLog (@ "The number entered is% d", numeroInserito); numeroInserito <numeroACaso ) { if (numeroInserito <numeroACaso) { "Troppo basso..." ; message = @ "too low ..."; ( numeroInserito> numeroACaso ) { } Else if (numeroInserito> numeroACaso) { "Troppo alto..." ; message = @ "Too high ..."; ( numeroInserito == numeroACaso ) { } Else if (numeroInserito numeroACaso ==) { "Bravo hai indovinato" ; message = @ "Bravo you guessed it"; + arc4random ( ) % 10 ; numeroACaso = 1 + arc4random ()% 10; "Numero pensato %d" , numeroACaso ) ; NSLog (@ "Number% d thought," numeroACaso); } [ [ UIAlertView alloc ] UIAlertView alertMessaggio * = [[UIAlertView alloc] "Responso" initWithTitle: @ "Response" message: message delegate: nil "OK" cancelButtonTitle: @ "OK" ] ; otherButtonTitles: nil]; ; [AlertMessaggio show]; ; [AlertMessaggio release]; "" ; numero.text @ = ""; } |
We're done!
Conclusions and considerations
, proprio perchè volevo lasciarlo il più semplice e snello possibile e, anche, per dimostrare che non sono elementi sempre necessari. This example does not make direct use of a UIView or UIViewController , because I wanted to leave it as simple and streamlined as possible and, also, to show that the elements are not always necessary. However, inserting objects directly in the window can have some sense in this example and other sporadic contexts. portano comunque benefici in tantissimi altri casi, ein alcuni sono praticamente indispensabili; come avremo modo di vedere in futuro. The use of UIView and UIViewController still bring benefits in many other cases, andin some are almost indispensable, as we will see in the future.
More ...
The first source that I went in his hands was written in Basic and consisted of a few lines of code, for enlightening me. It was a simple game that generated a random number from 1 to 10 and, through keyboard input, proved that the number entered was Maggione, less than or equal to the random number. Despite its rough simplicity remains, for me, one of the best examples - simple, fun and practical - to explain to those who do not know anything about programming what is meant actually for "computer program". So I decided to propose it for Apple iPhone, maybe will help someone ...
More ...
Latest Comments
Mark : Thank you very much, I've lit
I solved it by setting [cc_objc] / / OptionViewController.m - ...
Giovambattista Fazioli : @ Mark: I suggest you think a more correct approach. If you run the subclass of the tab ...
Mark : Excuse the spam .. I noticed that there is an error .. here is the correction [cc_objc] / PrimaClasse.h ** ** / # import ...
Marco : forgotten .. in [cci] OptionViewController [/ cci] for [cci] @ syntetize [/ cci] I put the delegate
louis : very clear and simple I have to admit that writing a pa hardly use delegates created by ...