4

Can somebody suggest to me how I might have a UIAlertView that has a list of buttons from an array. Basically, I want the following code but where myArray is an array of strings that come out as multiple of buttons.

UIAlertView *alert = [[UIAlertView alloc]initWithTitle: @"Title"
                                               message: nil
                                              delegate: self
                                     cancelButtonTitle:@"Cancel"
                                     otherButtonTitles: myArray,nil];
[alert show];

Of course, I can't just put in an array into the UIAlertView because it needs to be a string. How do I go about converting it to a string so that it doesn't come out as just one button? Or is that even how I should do it?

I've tried converting to a string using the following code:

NSString *listToBeUsed = [myArray componentsJoinedByString:@","];

But as I suspected, it didn't work. I was left with a long button with a list of strings and commas in-between them.

Any help would be appreciated.

[EDIT]

My array, as shown in the console:

( "888-555-5512", "555-522-8243", "(408) 555-3514" )

By the way, the phone numbers are from the simulators contacts list. No real people's numbers.

[EDIT]

So firstly, I create myArray:

NSMutableArray *myArray;

Then I get the saved NSUserDefault value in case the user added a phone number before (there will obviously be nothing if the user hasn't had a chance to add a phone number):

NSMutableArray *phoneNumber = [[NSUserDefaults standardUserDefaults] objectForKey:@"phoneNumber"];

Then I initiate myArray with the NSUserDefault. If nothing is saved in it, the table will be empty, if there are phone numbers in it, the table will display them because it displays myArray:

myArray = [[NSMutableArray alloc] initWithArray:phoneNumber];

In the - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath method I include the following code to display myArray in the table:

cell.numberLabel.text = [myArray objectAtIndex:indexPath.row];

When the user edits the phone numbers, I will do either of the following lines of code:

[myArray addObject:phoneNumberSelected];
[myArray removeObjectAtIndex:indexPath.row];

And then always save the array in NSUserDefaults so It can be accessed when the user returns:

[[NSUserDefaults standardUserDefaults] setObject:myArray forKey:@"phoneNumber"];

I hope I didn't miss anything out. I don't know if that was enough info, not enough or the wrong info.

Invalid Memory
  • 717
  • 7
  • 23
  • This seems to be duplicate of this http://stackoverflow.com/questions/2425732/objective-c-passing-in-variables-to-a-variable-length-method – Hussain Shabbir Nov 01 '13 at 10:20

2 Answers2

13

So lets say we have our NSArray declared like

NSMutableArray *myArray = [[NSMutableArray alloc] initWithObjects:@"888-555-5512", @"555-522-8243", @"(408) 555-3514"];

We will want to first create an instance of a UIAlertView but instead of setting any buttons to otherButtonTitles just leave it nil

UIAlertView *alert = [[UIAlertView alloc]initWithTitle: @"Title"
                                               message: nil
                                              delegate: self
                                     cancelButtonTitle:@"Cancel"
                                     otherButtonTitles: nil];

Now that we have our instance we will want to loop through our NSArray in a for loop to get each of the objects in it and we will assign each of those objects to an NSString which using the addButtonWithTitle: UIAlertView method we can set a new button on our UIAlertView with the title that we have assigned to our NSString.

for(NSString *buttonTitle in myArray) {
    [alert addButtonWithTitle:buttonTitle];
}

Then finally we can show the alert to the user.

[alert show];

Using NSString *listToBeUsed = [myArray componentsJoinedByString:@","]; will just join all your objects in your NSArray into an NSString separated "," so unless you're planning on making one button with that long string I which I suspect isn't your plan it will not work.

Just an observation

This is just an observation but the strings you have given seem like telephone numbers to make your code work better would it not be better to do something like

 for(NSString *buttonTitle in myArray) {
     if([[UIApplication sharedApplication] canOpenURL:[[NSURL URLWithString:[NSString stringWithFormat:@"tel://%@", buttonTitle] stringByReplacingOccurrencesOfString:@"-" withString:@""]]) {
         // The reason for this if statement is to check whether we can open a URL (Dialer) 
         // This is because what if the user was on an iPad or iTouch that can't use the dialer.
         [alert addButtonWithTitle:buttonTitle];
     } else {
         // Else do what ever you want to tell the user they can't make a phone call.
         break;
     }
 }

Like I say this is just an observation you can ignore this completely if you wish.

Popeye
  • 11,839
  • 9
  • 58
  • 91
  • OK, so when I put that code directly into the code that I have, I get the following caution `Collection expression type 'NSString *' may not respond to 'countByEnumeratingWithState:objects:count:'`. Do I need to replace something in that code? – Invalid Memory Nov 01 '13 at 09:17
  • @user2329585 How do you initialize your array? This indicates that `myArray` is an `NSString` and not an `NSArray`. – Popeye Nov 01 '13 at 09:19
  • `myArray = [[NSMutableArray alloc] initWithArray:phoneNumber];` (phoneNumber being a list of data in a table) I've just realised that it's an NSMutableArray, would that have anything to do with it? I forgot about that. – Invalid Memory Nov 01 '13 at 09:23
  • @user2329585 no that wouldn't really matter in this case unless your removing/adding objects but I will amend my code. – Popeye Nov 01 '13 at 09:28
  • @user2329585 how is `phoneNumber` initialized? – Popeye Nov 01 '13 at 09:29
  • Well, All I do is `NSMutableArray *myArray` and when the user selects a phone number to add, I add it using `[myArray addObject:phoneNumberSelected]`. It's then saved to NSUserDefaults `[[NSUserDefaults standardUserDefaults] setObject:details forKey:@"phoneNumber"]` and first thing after loading the table view I do `NSMutableArray *phoneNumber = [[NSUserDefaults standardUserDefaults] objectForKey:@"phoneNumber"]`. I'm not sure if I answered your question though... Am I doing it all completely wrong (as always!!)? – Invalid Memory Nov 01 '13 at 09:42
  • Right ok makes sense. You are adding a `NSMutableArray` (`phoneNumber`) into an `NSMutableArray` (`myArray`) so the for loop I have given you will not work. Give me 5 minutes and I will amend my code to resolve this. – Popeye Nov 01 '13 at 09:49
  • @user2329585 what is details and how is it created? – Popeye Nov 01 '13 at 10:03
  • Oh dear. My mistake. That's meant to be 'myArray'. I'll edit the comment to correct it. – Invalid Memory Nov 01 '13 at 10:08
  • Ok, I can't. But it's meant to be 'myArray'. – Invalid Memory Nov 01 '13 at 10:08
  • From the comments I am now confused. Please can you update your question with all the code and where it is. It might help me a little bit if you do so. – Popeye Nov 01 '13 at 10:13
  • See my edited question. By the way, thanks so much for your help so far! – Invalid Memory Nov 01 '13 at 10:46
  • @user2329585 to be honest with you as long as your array has some objects in it should work in that for loop then. I have just tested this code in the way you have detailed it and it works fine for me. – Popeye Nov 01 '13 at 10:58
  • After you said that, I scoured through me code for a mistake, and I found one. It's most annoying when it's a daft mistake. I'd forgotten to remove some of the code that turns the array into a string. Which is what you said the error suggested. Thank you so much for your time, the code you gave works beautifully with my mistake gone. If only Xcode could warn me about silly mistake then my life would be almost complete! I'll tick your answer and upvote. – Invalid Memory Nov 01 '13 at 11:08
0

You can use the addButtonWithTitle method to add the other buttons,with for statement.

    [alert addButtonWithTitle:@"AAA"];

For example refer below:-

NSArray *myArr = [NSArray arrayWithObjects:@"888-555-5512",@"456", nil];

UIAlertView *view = [[UIAlertView alloc] initWithTitle:@"ddd" message:@"dssd" delegate:self cancelButtonTitle:@"sdds" otherButtonTitles:nil];

for(NSString* number in myArr)
    [view addButtonWithTitle:number];

[view show];
Hussain Shabbir
  • 14,801
  • 5
  • 40
  • 56
hackcat
  • 19
  • 2