Search code examples
iphoneiosnsarrayuiactionsheet

How to display NSArray objects in the buttons of a UIActionSheet


I'm trying to populate a UIActionSheet button list with the result from an NSArray. I have cityTitleNodeArray which is an NSArray (nslog output below). At the moment it only displays the first item in the NSArray as the first button in the UIActionSheet

I would like it to look like this (except the array info should all come from cityTitleNodeArray) :

enter image description here

   NSArray *array = [[NSArray alloc] initWithObjects: cityTitleNodeArray, @"city2", @"city3", @"city4", @"city5", @"city6", @"city7", @"city8",@"city9", @"city10", @"city11", @"city12", @"city13", @"city14", @"city15", @"city16", nil];


    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Title Here"
                                                             delegate:self
                                                    cancelButtonTitle:nil
                                               destructiveButtonTitle:nil
                                                    otherButtonTitles:nil];

    for (NSString *title in array) {
        [actionSheet addButtonWithTitle:title];
    }

    [actionSheet addButtonWithTitle:@"Cancel"];
    actionSheet.cancelButtonIndex = [array count];

    [actionSheet showInView:self.view];




  -(void)requestCityData {

 NSError *requestError = nil;

 NSString *savedValue = [[NSUserDefaults standardUserDefaults] stringForKey:@"token"];

 NSString *stringWithToken = [NSString stringWithFormat:@"%@&token=%@",kCityURL, savedValue];

 NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:stringWithToken]];

 NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&requestError];

 NSError *jsonParsingError = nil;

 if (requestError) {
 NSLog(@"sync. request failed with error: %@", requestError);
 }
 else {
 // handle data
 NSDictionary *publicData =  [NSJSONSerialization JSONObjectWithData:response options:0 error:&jsonParsingError];

     publicCityDataArray = [publicData objectForKey:@"data"];

     for (NSDictionary *publicCityDataDict in publicCityDataArray) {

         cityTitleNodeArray = [publicCityDataDict objectForKey:@"name"];
         NSLog(@"cityTitleNodeArray from requestCityData output is %@",cityTitleNodeArray);

     }

 }

}

the cityTitleNodeArray output is:

`cityTitleNodeArray` from `requestCityData` output is Roma
2013-09-16 12:49:28.001 1000 [3980:907] cityTitleNodeArray from requestCityData output is Milano
2013-09-16 12:49:28.002 1000 [3980:907] cityTitleNodeArray from requestCityData output is Rimini
2013-09-16 12:49:28.002 1000 [3980:907] cityTitleNodeArray from requestCityData output is Venezia
2013-09-16 12:49:28.003 1000 [3980:907] cityTitleNodeArray from requestCityData output is Firenze
2013-09-16 12:49:28.003 1000 [3980:907] cityTitleNodeArray from requestCityData output is Napoli

Solution

  • NSArray *array = [[NSArray alloc] initWithObjects:
                     @"1st Button",
                     @"2nd Button",
                     @"3rd Button",
                     @"4th Button",
                     nil];
    
    UIActionSheet* actionSheet = [[UIActionSheet alloc] init];
    
    actionSheet.title = @"Cities Name";
    
    actionSheet.delegate = self;
    
    for(int i=0;i<[array count];i++)
    
    {
    
    [actionSheet addButtonWithTitle:[array objectAtIndex:i]];
    
    }
    
    actionSheet.cancelButtonIndex = [actionSheet addButtonWithTitle:@"Cancel"];
    
    [actionSheet showInView:[UIApplication sharedApplication].keyWindow];