something:something:something Method Format?

2020-04-14 07:10发布

-(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?

5条回答
手持菜刀,她持情操
2楼-- · 2020-04-14 07:14

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_objectivec/

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

查看更多
何必那么认真
3楼-- · 2020-04-14 07:26

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).

查看更多
Lonely孤独者°
4楼-- · 2020-04-14 07:31

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 ]   
}
查看更多
Deceive 欺骗
5楼-- · 2020-04-14 07:38

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

查看更多
虎瘦雄心在
6楼-- · 2020-04-14 07:40

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.

查看更多
登录 后发表回答