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);
}

6 comments to "iPhone code snippet: UIAlertView"

  1. July 12, 2009 Corninos:

    Hello I have a question ..
    I copied your line of code (the last window)
    but I get this error:

     error: 'alertView' undeclared (first use in this function) 
    error: expected ';' before ':' token

    Whi?

  2. July 13, 2009 Giovambattista Fazioli :

    @ Corninos: in. H file in your class (I do not know where you entered the code) you must insert the function prototype, namely:

    1
    void ) alertView : ( UIAlertView * ) alertView clickedButtonAtIndex : ( NSInteger ) buttonIndex; - (Void) alertView: (UIAlertView *) alertView clickedButtonAtIndex: (NSInteger) buttonIndex;

    In addition, careful, you could have made a typo somewhere. If necessary incollami your whole code or send me a email.

  3. July 13, 2009 Corninos:

    Hello and thank you for having responded to me in the end I had a typo, or copying.
    Within the second alertView you wrote:

    1
    ; myAlterA.tag = 2;

    and so I did not work properly in:

    1
    ; myAlterB.tag = 2;

    and everything seems to work ..
    Now I have made you one more question "stupid" but if I wanted to change the background color of a button or label .. or even just the title of a button as I do? I have tried:

    1
    "nuovotitolo" ; pulsante.title @ = "nuovotitolo";

    but does not work, then I tried

    1
    @nuovotitolo "]; [SetTitleLabel button: @ nuovotitolo "];

    I thank you in advance!

  4. July 13, 2009 Giovambattista Fazioli :

    (dislessia @ Corninos: in fact half my fault: I realized that I had written Alter instead of Alert (dyslexia :) I corrected the code in the post). , agendo su cancelButtonTitle e otherButtonTitles ma non puoi alterare lo stile, essendo elementi di sistema. For the titles and the style of the buttons, if you're referring to ' UIAlertView , you can change the labels during the creation of ' UIAlertView , acting on cancelButtonTitle and otherButtonTitles but you can not alter the style, being elements of the system.
    Unless you're referring to a simple button UIButton ... if I understand the question?!

  5. July 13, 2009 Corninos:

    @ Giovambattista Fazioli:
    I riferifo a generic button .. UIButton is an OFF-TOPIC ..

  6. 13 July 2009 Giovambattista Fazioli :

    @ Corninos: If you're referring to a button UIButton the label is changed as follows:

    1
    @ "Nuova Label" forState : UIControlStateNormal ] ; [Button setTitle: @ "New Label" Forst: UIControlStateNormal];

    The background, however, you can change it in this way, although it depends on what type of button you are using:

    1
    [ UIColor redColor ] ] ; [Button setBackgroundColor: [UIColor redColor]];

Leave a comment

XHTML TAG PERMIT: <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 [cc_actionscript][/cc_actionscript] // Actionscript [cc_actionscript3][/cc_actionscript3] // Actionscript 3 [cc_css][/cc_css] // CSS Style Sheet [cc_html][/cc_html] // HTML [cc_js][/cc_js] // Javascript [cc_objc][/cc_objc] // Objective-C [cc_php][/cc_objc] // PHP [cc_sql][/cc_sql] // SQL 


Stop SOPA