0

Say for example this is my C ( & Objective-C ) method as follows.

void ALERT(NSString *title, NSString *message,NSString *canceBtnTitle,id delegate,NSString *otherButtonTitles, ... )
{
    // HERE I CAN ACCESS ALL THOSE ARGUMENTS
    // BUT I AM NOT SURE How to access additional arguments, supplied using ... ?

UIAlertView *alertView=[[UIAlertView alloc] initWithTitle:title 
                                                  message:message 
                                                 delegate:delegate 
                                        cancelButtonTitle:canceBtnTitle 
                                        otherButtonTitles:// how to pass those params here?];
}

As you can notice that, I also have to pass those parameters in UIAlertView's init method. I am not sure how to send those parameters into otherButtonTitles. I can invoke this method by following ways.

ALERT(@"My Alert Title",@"Alert Subtitle",@"YES",viewCtr,@"No",@"May Be",@"Cancel",nil);

ALERT(@"Alert Title",@"Alert Subtitle",@"OK",viewCtr,@"Cancel",nil);

ALERT(@"Alert Title",@"Alert Subtitle",@"OK",viewCtr,nil);

ALERT(@"Alert Title",@"Alert Subtitle",@"OK",viewCtr,@"Option1",@"Option2",nil);
halfer
  • 19,824
  • 17
  • 99
  • 186
sagarkothari
  • 24,520
  • 50
  • 165
  • 235

2 Answers2

2

Sounds like you need to get know va_arg (and va_list, va_start, va_end).

Here's a tutorial on the subject.

Also, a fine Apple tech note entitled "How can I write a method that takes a variable number of arguments, like NSString's +stringWithFormat:?"

Edited to add:

Sounds like you want to do va_copy.

Ahh, here is a related question.

Community
  • 1
  • 1
Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215
  • Yep, I got the idea how to access them, But I am not sure, how to pass them as it is to the next function as edited within question. – sagarkothari Nov 25 '11 at 09:09
0

Hmmm ! I have gone through Wiki link

I also learned something from Michael' Answer in this post.

I have came up with following solution.

void ALERT(NSString *title, NSString *message,NSString *canceBtnTitle,id delegate,NSString *otherButtonTitles, ... )
{
    UIAlertView *alertView=[[UIAlertView alloc] initWithTitle:title 
                                                      message:message 
                                                     delegate:delegate 
                                            cancelButtonTitle:canceBtnTitle 
                                            otherButtonTitles:nil
                            ];

    va_list args;
    va_start(args, otherButtonTitles);
    NSString *obj;
    for (obj = otherButtonTitles; obj != nil; obj = va_arg(args, NSString*))
        [alertView addButtonWithTitle:obj];
    va_end(args);

    [alertView show];
}
Community
  • 1
  • 1
sagarkothari
  • 24,520
  • 50
  • 165
  • 235