我正在使用NSUserDefaults保存NSMutableArray。然后在另一个视图控制器中,我将从NSUserDefaults中检索NSMutableArray。所以我想要做的是从该数组中的第一个NSDictionary中检索NSString,然后将其作为按钮添加到我的UIActionSheet中。然后我会循环它,所以从NSMutableArray的第二个NSDictionary中检索第二个NSString。我还想让它们保持正确的顺序。那么来自NSDictionary的NSString是NSMutableArray中的objectAtIndex:0,是UIActionSheet中的第一个按钮吗? 这是可能的,如果是这样,我将如何实现这一目标?
非常感谢!
编辑(此代码存在泄漏)。 Xcode用粗线表示,但我不确定,我在这里泄漏了什么或做错了什么?
NSUserDefaults *prefs =[NSUserDefaults standardUserDefaults];
**NSMutableArray *accountsArray = [[prefs objectForKey:@"AccountArray"] mutableCopy];**
if ([accountsArray count] >= 2) {
UIActionSheet *playingSheet = [[UIActionSheet alloc] initWithTitle:@"Hi?" delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
[playingSheet showInView:self.view];
for(NSDictionary * dict in accountsArray)
{
NSString *name = [dict objectForKey:@"Name"];
[playingSheet addButtonWithTitle:name];
}
[playingSheet release];
}
EDIT2: 当我运行以下代码时,在我的应用程序中运行analyze时会收到警告。无论如何,错误是,粗体线中的对象的潜在泄漏并存储到accountsArray中。该行是edit2帖子中的第二行。
NSUserDefaults *prefs =[NSUserDefaults standardUserDefaults];
**NSMutableArray *accountsArray = [[prefs objectForKey:@"AccountArray"] mutableCopy];**
if ([accountsArray count] >= 2) {
UIActionSheet *playingSheet = [[UIActionSheet alloc] initWithTitle:@"Hi?" delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
[playingSheet showInView:self.view];
for(NSDictionary * dict in accountsArray)
{
NSString *name = [dict objectForKey:@"Name"];
[playingSheet addButtonWithTitle:name];
}
[playingSheet release];
}
答案 0 :(得分:2)
这样的东西?
UIActionSheet * sheet = [[UIActionSheet alloc] initWithTitle .......]
for(NSDictionary * dict in myMutableArray)
{
NSString * str = [dict objectForKey:@"MyStringKey"];
[sheet addButtonWithTitle:str];
}