3

我正在尝试UIActionSheet使用来自NSArray. 我有 cityTitleNodeArray 这是一个NSArraynslog下面的输出)。目前它只显示第一个项目NSArray作为第一个按钮UIActionSheet

我希望它看起来像这样(除了数组信息都应该来自 cityTitleNodeArray):

在此处输入图像描述

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

     }

 }

}

cityTitleNodeArray输出是:

`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
4

3 回答 3

8
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];
于 2013-09-16T12:22:32.353 回答
2

用于从数组中设置按钮的标题

UIActionSheet *actionSheet=[[UIActionSheet alloc]initWithTitle:@"Title" delegate:self cancelButtonTitle:@"Cancel" 破坏性ButtonTitle:nil otherButtonTitles:[array objectAtIndex:0],[array objectAtIndex:1],[array objectAtIndex:2],零];

[actionSheet showInView:self.view];

在委托方法中

pragma mark Action Sheet 委托方法

-(void) actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{

switch (buttonIndex) {
    case 0:
    { 
        //Actions on pressing Button 1
    }

        
    
        break;
        
    case 1:
    {
 //Actions on pressing Button 2
    }
        break;
        
    case 2:
    {
      //Actions on pressing Button 3
    }
        break;
        
    default:
        break;
}

}

于 2013-09-16T12:45:19.447 回答
1

试试这个

NSArray *array = [[NSArray alloc] initWithObjects:
                      [NSString stringWithString:@"1st Button"],
                      [NSString stringWithString:@"2nd Button"],
                      [NSString stringWithString:@"3rd Button"],
                      [NSString stringWithString:@"4th Button"],
                      nil];
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Title Here"
                                                         delegate:self
                                                cancelButtonTitle:nil
                                           destructiveButtonTitle:nil
                                                otherButtonTitles:nil];

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

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

[actionSheet showInView:self.view];
于 2013-09-16T12:07:11.747 回答