Articles Tagged 'UIAlertView'

How to develop in PHP with Xcode and Objective-C

How many programmers to use, I also build my own library of functions ready to be reused in multiple projects and multiple contexts. Let me change the title of this post, maybe a little 'risky but, nevertheless, as we shall see, not far from reality.

In Objective-C you can write and call C / C + +, including the assembly for that matter. This characteristic makes it a very versatile language and, in some respects, phenomenal. On the one hand, you can use and appreciate the purely syntax of Objective-C, on the other you can run faster porting code written in ANSI C (perhaps for Digital Unix or Sun) and can comfortably fit into our iPhone or iPad applications; not to mention all the BSD kernel is already available on Mac OS X!

Continued ...

iPhone SecondApp: Guess the number - Part 2

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, Xcode wizard. At the end of this post, then, we have an application identical in all respects, to that achieved in the first part, with the difference that we will achieve all our visual components, including the main Window, completely to 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 weighing 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:

newproject

Now let's delete everything related to Interface Builder. : eliminatelo anche dal file system, quindi selezionate Also Move to Trash . Delete the file MainWindow.xib , located in the folder Resources : also delete it from the file system, 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 box Main base file name:

deleteib

At this point we no longer have any Windows, 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 this:

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 missed the point
/ / Delegate to the JPA, 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 create the main Window in 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 have done in Interface Builder bye bye
[ [ UIWindow alloc ] initWithFrame : windowRect ] ; MainWindow UIWindow * = [[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 you get a yellow window 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;
numeroACaso int;
}

nonatomic, retain ) UIWindow * window; @ Property (nonatomic, retain) UIWindow * window;

void ) controllaNumero; - (Void) getNumber;

@ End

, in quanto non stiamo usando Interface Builder. Again we have prepared the method definition controllaNumero , like last time, but we have eliminated the indication IBAction , because you are not using Interface Builder.

We build the interface from code

It 'time to create code using all components of our interface. ed inseriamo il seguente codice: Let's go back to 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 the 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" Forst: 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, just after the [mainWindow release]; insert:

1
+ arc4random ( ) % 10 ; numeroACaso arc4random = 1 + () 10%;

Now we can do is implement the method controllaNumero , which will be identical (apart from the prototype) to that 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 arc4random = 1 + () 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 , just because I wanted to leave it as simple and streamlined as possible and, also, to show that the elements are not always necessary. However insert 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 bring benefits in many other cases, however, some are virtually indispensable ein, as we will see in the future.

Continued ...

iPhone FirstApp: Guess the number - Part 1

The first source that I rose in my hands was written in Basic and consisted of a few lines of code, for enlightening me. It was a simple game that generates a random number from 1 to 10 and, through keyboard input, verify that the number entered was Maggioni, 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 ...

Continued ...

iPhone code snippet: UIAlertView

The class UIAletrView allows you to display a modal window (animated) to inform and / or ask you something. The UIAlterView (view alerts) are very comfortable and easy to use. The management of user response is solved by intercepting the index of the button pressed. To view a simple UIAlterView just write:

1
2
3
4
/ / Alert with a simple button, with no callback
[ [ UIAlertView alloc ] initWithTitle : @ "Titolo" message : @ "Messaggio" cancelButtonTitle : @ "Annulla" otherButtonTitles : nil ] ; UIAlertView MyAlert * = [[UIAlertView alloc] initWithTitle: @ "Title" message: @ "Message" cancelButtonTitle: @ "Cancel" otherButtonTitles: nil];
; [MyAlert show];
; [MyAlert release];

And 'possible to add additional buttons otherButtonTitles :

1
2
3
4
/ / Alert with two simple buttons, no callback
[ [ UIAlertView UIAlertView MyAlert * = [[UIAlertView
; [MyAlert show];
; [MyAlert release];

: To determine which button was pressed, simply add the delegate:self object initialization UIAlterView :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
/ / Simple alert with two buttons and callbacks
[ [ UIAlertView alloc ] initWithTitle : @ "Titolo" message : @ "Messaggio" delegate : self cancelButtonTitle : @ "Annulla" otherButtonTitles : @ "Ok" , nil ] ; UIAlertView MyAlert * = [[UIAlertView alloc] initWithTitle: @ "Title" message: @ "Message" delegate: self cancelButtonTitle: @ "Cancel" otherButtonTitles: @ "OK", nil];
; [MyAlert show];
; [MyAlert release];

/ / The callback looks like this

void ) alertView : ( UIAlertView * ) alertView clickedButtonAtIndex : ( NSInteger ) buttonIndex { - (Void) alertView: (UIAlertView *) alertView clickedButtonAtIndex: (NSInteger) {buttonIndex
buttonIndex ) { switch (buttonIndex) {
: case 1:
/ / Code
break;
}
}

The callback alertView need to have that name, for more information see UIAlertViewDelegate

punteranno sempre a quella. With only the callbck alertView:clickedButtonAtIndex more UIAletrView will always point to that. Using the tag of the field UIAletrView , we can determine what the "caller":

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/ / Simple alert with two buttons and callbacks
[ [ UIAlertView alloc ] initWithTitle : @ "Titolo" message : @ "Messaggio" delegate : self cancelButtonTitle : @ "Annulla" otherButtonTitles : @ "Ok" , nil ] ; UIAlertView MyAlert * = [[UIAlertView alloc] initWithTitle: @ "Title" message: @ "Message" delegate: self cancelButtonTitle: @ "Cancel" otherButtonTitles: @ "OK", nil];
; myAlertA.tag = 1;
; [MyAlert show];
; [MyAlert release];

/ /
[ [ UIAlertView alloc ] initWithTitle : @ "Titolo" message : @ "Messaggio" delegate : self cancelButtonTitle : @ "Annulla" otherButtonTitles : @ "Ok" , nil ] ; UIAlertView myAlertB * = [[UIAlertView alloc] initWithTitle: @ "Title" message: @ "Message" delegate: self cancelButtonTitle: @ "Cancel" otherButtonTitles: @ "OK", nil];
; myAlertA.tag = 2;
; [MyAlertB show];
; [MyAlertB release];

/ / Multiple management UIAlertView

void ) alertView : ( UIAlertView * ) alertView clickedButtonAtIndex : ( NSInteger ) buttonIndex { - (Void) alertView: (UIAlertView *) alertView clickedButtonAtIndex: (NSInteger) {buttonIndex
"Alert Tag: %i" , alertView.tag ) ; NSLog (@ "Alert Tags:% i", alertView.tag);
"buttonIndex: %i" , buttonIndex ) ; NSLog (@ "buttonIndex:% i", buttonIndex);
}

Continued ...

iPhone Objective-C syntax equivalence

The syntax with square brackets in Objective-C is one of the reasons most frustration for those coming from other languages ​​(for objects). However, get used to it, you realize how readable the code becomes too with this mix of classical and more distinctly syntax Smalltalk. For example we see how the same procedure can be written in both syntax. Take the initialization of a UIAlertView :

1
2
3
4
5
6
7
8
[ [ UIAlertView alloc ] initWithTitle : @ "Titolo" UIAlertView MyAlert * = [[UIAlertView alloc] initWithTitle: @ "Title"
"Messaggio" message: @ "Message"
delegate: self
"Annulla" cancelButtonTitle: @ "Cancel"
"Ok" , nil ] ; otherButtonTitles: @ "OK", nil];
; myAlert.tag = 1;
; [MyAlert show];
; [MyAlert release];

The setting of the tag potavamo it even so:

1
2
3
4
5
6
7
8
[ [ UIAlertView alloc ] initWithTitle : @ "Titolo" UIAlertView MyAlert * = [[UIAlertView alloc] initWithTitle: @ "Title"
"Messaggio" message: @ "Message"
delegate: self
"Annulla" cancelButtonTitle: @ "Cancel"
"Ok" , nil ] ; otherButtonTitles: @ "OK", nil];
1 ] ; [MyAlert SETTING: 1];
; [MyAlert show];
; [MyAlert release];

Same thing in reading during the event:

1
2
3
4
5
void ) alertView : ( UIAlertView * ) alertView clickedButtonAtIndex : ( NSInteger ) buttonIndex { - (Void) alertView: (UIAlertView *) alertView clickedButtonAtIndex: (NSInteger) {buttonIndex
alertView.tag == 1 ) { // ... if (alertView.tag == 1) {/ / ...
/ / Or
alertView tag ] == 1 ) { // ... if ([alertView tag] == 1) {/ / ...
}

Note: Not all properties or methods have this dual syntax. In the case of ' UIAlertView we have this dual functionality. For other objects, or in other cases, however, only one of the two might work ... but do not ask why (depending on how the developers have defined the interface).

Continued ...

iPhone: the most useful tag property

All objects that derive from <a target="_blank" href="http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UIView_Class/UIView/UIView.html#//apple_ref/occ/cl/UIView">UIView</a> inherit the useful properties <a target="_blank" href="http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UIView_Class/UIView/UIView.html#//apple_ref/doc/uid/TP40006816-CH3-SW25">tag</a> This property is a real user-data (a "place" that the developer used for general purposes) of type <a target="_blank" href="http://developer.apple.com/iphone/library/documentation/Cocoa/Reference/Foundation/Miscellaneous/Foundation_DataTypes/Reference/reference.html#//apple_ref/doc/c_ref/NSInteger">NSInteger</a> so where we can store only numbers.

The uses to which they can obviously depends on the circumstances however, it is useful to identify a particular object to taking a common occurrence. For example, imagine you have two <a target="_blank" href="http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UIAlertView_Class/UIAlertView/UIAlertView.html">UIAlertView</a> respond to the same event:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/ / First alert
[ [ UIAlertView alloc ] initWithTitle : @ "Primo" message : @ "Primo Alert" delegate : self cancelButtonTitle : @ "OK" otherButtonTitles : nil , nil ] ; UIAlertView * alert = [[UIAlertView alloc] initWithTitle: @ "First" message: @ "First Alert" delegate: self cancelButtonTitle: @ "OK" otherButtonTitles: nil, nil];
; [Alert show];
; [Alert release];

/ / ...

/ / Second alert
[ [ UIAlertView alloc ] initWithTitle : @ "Secondo" message : @ "Secondo Alert" delegate : self cancelButtonTitle : @ "OK" otherButtonTitles : nil , nil ] ; UIAlertView * alert = [[UIAlertView alloc] initWithTitle: @ "Second" message: @ "Second Alert" delegate: self cancelButtonTitle: @ "OK" otherButtonTitles: nil, nil];
; [Alert show];
; [Alert release];

/ / Event
/ / OK button Cliccandi of this function will be called Alter
void ) alertView : ( UIAlertView * ) alertView clickedButtonAtIndex : ( NSInteger ) buttonIndex { - (Void) alertView: (UIAlertView *) alertView clickedButtonAtIndex: (NSInteger) {buttonIndex
/ / Todo
}

Note: code examples in C + + you will find directions. This is not entirely correct because, as you know, the language is Objective-C. This is due to the fact that the plugins I use to view the source code does not support Objective-C and C + + is the one that best approaches him.

How to differentiate between the two alert? Precisely using the property tag. After creating the alert just enter:

1
2
3
4
5
6
7
8
9
10
11
12
13
/ / First alert
[ [ UIAlertView alloc ] initWithTitle : @ "Primo" message : @ "Primo Alert" delegate : self cancelButtonTitle : @ "OK" otherButtonTitles : nil , nil ] ; UIAlertView * alert = [[UIAlertView alloc] initWithTitle: @ "First" message: @ "First Alert" delegate: self cancelButtonTitle: @ "OK" otherButtonTitles: nil, nil];
1 ] ; // imposto il tag di questo Alert ad 1 [Alert SETTING: 1] / / set the tag of this Alert to 1
; [Alert show];
; [Alert release];

/ / ...

/ / Second alert
[ [ UIAlertView alloc ] initWithTitle : @ "Secondo" message : @ "Secondo Alert" delegate : self cancelButtonTitle : @ "OK" otherButtonTitles : nil , nil ] ; UIAlertView * alert = [[UIAlertView alloc] initWithTitle: @ "Second" message: @ "Second Alert" delegate: self cancelButtonTitle: @ "OK" otherButtonTitles: nil, nil];
2 ] ; // imposto il tag di questo Alert ad 2 [Alert SETTING: 2] / / set the tag of this Alert to 2
; [Alert show];
; [Alert release];

Now modify the event in order to understand which Alert has been closed:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
/ / Event
/ / OK button Cliccandi of this function will be called Alter
void ) alertView : ( UIAlertView * ) alertView clickedButtonAtIndex : ( NSInteger ) buttonIndex { - (Void) alertView: (UIAlertView *) alertView clickedButtonAtIndex: (NSInteger) {buttonIndex
alertView.tag ) { switch (alertView.tag) {
: case 1:
/ / First Alert
break;
: case 2:
/ / According Alert
break;
default:
break;
}
}

Exact same technique can be used if we have a series of <a target="_blank" href="http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UIButton_Class/UIButton/UIButton.html">UIButton</a> run-time. For example:

1
2
3
4
5
6
7
8
9
10
unsigned int i = 0 ; i < 10 ; i ++ ) { for (unsigned int i = 0; i <10; i + +) {
[ [ UIButton buttonWithType : UIButtonTypeCustom ] initWithFrame : CGRectMake ( i * 20 , i * 20 , 20 , 20 ) ] ; UIButton * myButton = [[UIButton buttonWithType: UIButtonTypeCustom] initWithFrame: CGRectMake (i * 20, * 20, 20, 20)];
@ "But" forState : UIControlStateNormal ] ; [MyButton setTitle: @ "But," Forst: UIControlStateNormal];
i ] ; // imposto il tag [MyButton SETTING: i]; / / set the tag

/ / The same event for all
self action : @selector ( onTouchUpInside : ) forControlEvents : UIControlEventTouchUpInside ] ; [MyButton addTarget: self action: @ selector (onTouchUpInside:) forControlEvents: UIControlEventTouchUpInside];

mybutton ] ; [Self.view addSubview: myButton];
}

(dove eseguiamo un casting <a target="_blank" href="http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UIView_Class/UIView/UIView.html#//apple_ref/occ/cl/UIView">UIView</a> In the event onTouchUpInside recover from the sender (where we perform a casting <a target="_blank" href="http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UIView_Class/UIView/UIView.html#//apple_ref/occ/cl/UIView">UIView</a> the property <a target="_blank" href="http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UIView_Class/UIView/UIView.html#//apple_ref/doc/uid/TP40006816-CH3-SW25">tag</a>

1
2
3
4
void ) onTouchUpInside : ( id ) sender { - (Void) onTouchUpInside: (id) sender {
( ( UIView * ) sender ) .tag; button_tag unsigned int = ((UIView *) sender). tags;
/ / Todo
}

Continued ...


Stop SOPA