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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With