如何将NSDictionary中的对象添加到UIActionSheet UIButton列表的NSArray中

时间:2013-09-17 09:27:56

标签: ios objective-c nsarray nsdictionary uiactionsheet

我的字典

    myDictionary = [[publicDataCityArray valueForKey:@"data"] valueForKey:@"name"];

    NSLog(@"myDictionary is %@ myDictionary count is %i",   myDictionary,  [myDictionary count]);

NSLog输出:

myDictionary is (
    Roma,
    Milano,
    Rimini,
    Venezia,
    Firenze,
    Napoli,
    Cesenatico,
    Cervia,
    Recco,
    "Abano Terme",
    "Abbadia Cerreto",
    "Abbadia Lariana",
    "Abbadia San Salvatore",
    Abbasanta,
    Abbateggio,
    Abbiategrasso,
    Abetone,
    Abriola,
    Acate,
    Accadia,
    Acceglio,
    Accettura,
    Acciano,
    Accumoli,
    Acerenza,
    Acerno,
    Acerra,
    "Aci Bonaccorsi",
    "Aci Castello",
    "Aci Catena",
    "Aci Sant'Antonio",
    Acireale,
    Acquacanina,
    Acquafondata,
    Acquaformosa,
    Acquafredda,
    Acqualagna,
    "Acquanegra Cremonese",
    "Acquanegra sul Chiese",
    Acquapendente
) myDictionary count is 40

我正在尝试填充UIActionSheet数组:

-(void) someMethod{

    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
        NSArray *publicDataCityArray =  [NSJSONSerialization JSONObjectWithData:response options:0 error:&jsonParsingError];

        myDictionary = [[publicDataCityArray valueForKey:@"data"] valueForKey:@"name"];

        NSLog(@"myDictionary is %@ myDictionary count is %i",   myDictionary,  [myDictionary count]);

        for (NSDictionary *publicCityDataDict in publicCityDataArray) {

            arrayCitiesActionSheet = [[NSArray alloc] initWithObjects: myDictionary, @"city5", @"city6", @"city7", @"city8",@"city9", @"city10", @"city11", @"city12", @"city13", @"city14", @"city15", @"city16", nil];


           // arrayCitiesActionSheet = [myDictionary allValues];

        }

    }

    UIActionSheet* actionSheet = [[UIActionSheet alloc] init];

    actionSheet.title = @"SELECT A CITY";

    actionSheet.delegate = self;

    for(int i=0;i<[myDictionary count];i++)

    {

        [actionSheet addButtonWithTitle:[arrayCitiesActionSheet objectAtIndex:i]];

    }

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

    [actionSheet showInView:[UIApplication sharedApplication].keyWindow];

}

1 个答案:

答案 0 :(得分:1)

您是否尝试将myDictionary作为数组,

试试这个,

 for(int i=0;i<[myDictionary count];i++)
    {
        [actionSheet addButtonWithTitle:[arrayCitiesActionSheet objectAtIndex:i]];
    }

替换为

for(int i=0;i<[myDictionary count];i++)
        {
            [actionSheet addButtonWithTitle:[myDictionary objectAtIndex:i]];
        }