我正在尝试使用UIActionSheet
的结果填充NSArray
按钮列表。我有cityTitleNodeArray,它是NSArray
(下面是nslog
输出)。目前它只显示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
答案 0 :(得分: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];
答案 1 :(得分:2)
用于设置数组中按钮的标题
UIActionSheet * actionSheet = [[UIActionSheet alloc] initWithTitle:@“Title”delegate:self cancelButtonTitle:@“Cancel”destructiveButtonTitle:nil otherButtonTitles:[array objectAtIndex:0],[array objectAtIndex:1],[array objectAtIndex: 2],nil];
[actionSheet showInView:self.view];
在委托方法
中- (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;
}
}
答案 2 :(得分: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];