views:

118

answers:

5
-(void) alertView: ( UIAlertView *) alertView 
         clickedButtonAtIndex: ( NSInteger ) buttonIndex {
      // do stuff
      // if you want the alert to close, just call [ alertView release ]   
}

Can someone explain this method? I'm used to methods that are like "-(IBAction)buttonPress:(id)sender" but this one has three. What do each of those mean?

MethodName : ReturnedType : InputType is this right?

A: 

It is a delegate protocol method implementation.
You can find some details about that pattern at iPhone Dev Central.

The class that implements that method acts as a delegate of an UIAlertView.
This way you can customize the behavior of an instance of a class without subclassing.

weichsel
+4  A: 

It's actually:

-(return type) methodName:(param1 type)param1 moreMethodName:(param2 type)param2

Etc, with as many parameters as you want. So that method is called alertView:clickedButtonAtIndex: -- it just has its parameters embedded. It's the equivalent, in a more "normal" language, of alertViewClickedButtonAtIndex(UIAlertView *alertView, NSInteger buttonIndex)

For a pretty good primer on Obj-C syntax, check out: http://www.cocoadevcentral.com/d/learn%5Fobjectivec/

For info on that particular method, check out this document.

Ian Henry
+3  A: 

Objective-C methods with arguments:

A method with no arguments:

-(void)methodName;

Signature is methodName.

A method with 1 argument:

-(void)methodName:(ArgumentType *)anArgument;

Signature is methodName:.

A method with 2 arguments

-(void)methodName:(ArgumentType1 *)argument1 andArgumentType2:(ArgumentType2 *)argument2;

Signature is methodName:andArgumentType2:

So this is method is a method of 2 arguments: a UIAlertView object and an NSInteger (not an object, simply syntactic sugar for either an int or long depending on your system).

The UIAlertView is the alert view whose delegate has been set to the object of this class. It's usually set when the alert view is created.

The buttonIndex is the index of the button on the UIAlertView that the user touched. This method is called when that button is clicked. By default, nothing is done, and the alert simply vanishes.

You use this method if you want an alert with buttons to pop up, and, when the user clicks on one of the buttons, have the class that invoked the alert do something (possibly different things depending on which button was clicked).

executor21
A: 

Its a method with two input arguments.

Similar to:

void someMethod(int i, int j){}

It always a good idea to pick up a book and get the basics right instead of learning in bit and pieces. Trust me :)

Mihir Mathuria
A: 

When writing my objective-C I prefer to format the method as follows as I think it makes the separation of return type and parameters clearer:

-(void)                                          // return type
alertView:(UIAlertView *) alertView              // param1
clickedButtonAtIndex:(NSInteger) buttonIndex     // param2
{
      // do stuff
      // if you want the alert to close, just call [ alertView release ]   
}
Robert Tuck