2011-09-01 98 views
0

我在读出可变字典中的可变数组时遇到了问题。我试图将其读出来,然后将其放入标签中。这是我的数据是如何组织的。我有一个可变的字典阵列。然后在我的tableview中,我将对象放在索引路径中,并将它设置为等于应用程序委托中的字典。然后在细节视图中加载我从数组中取出的字典,然后从中读取并将值放入标签中。我在字典中工作的所有字符串,但数组不会读出来,你能不能帮我如何从NSMutableDictionary中读取NSMutableArray

RootViewController.m

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    SurveyAppAppDelegate *surveyAppAppDelegate = (SurveyAppAppDelegate*)[[UIApplication sharedApplication] delegate]; 

    SurveyViewer *surveyViewController = [[SurveyViewer alloc] initWithNibName:@"SurveyViewer" bundle:nil]; 
    surveyAppAppDelegate.arrayCount = [[self surveyArray] objectAtIndex:indexPath.row]; 


    [self.navigationController pushViewController:surveyViewController animated:YES]; 
    [surveyViewController release]; 
} 

在我的应用程序代表它被设置为NSDictionary的。

然后在我的详细视图控制器我这样做

injuredArray = [surveyAppAppDelegate.arrayCount objectForKey:@"Injured Part"]; 

    if ([injuredArray count] >= 1) { 
     for (int i = 0; i<=([injuredArray count]-1); i++) { 
      myLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 105+shift, 139, 15)]; 
      myLabel.text = [injuredArray objectAtIndex:i]; 
      myLabel.backgroundColor = [UIColor clearColor]; 
      myLabel.font = [UIFont fontWithName:@"Helvetica" size: 14.0]; 
      myLabel.textColor = [UIColor blackColor]; 
      myLabel.textAlignment = UITextAlignmentLeft; 
      [self.theScroller addSubview:myLabel]; 
      shift = shift+20; 
     } 
    } 

在我的应用程序委托我成立arrayCount这样

.H

#import <UIKit/UIKit.h> 

    @class RootViewController; 

    @interface SurveyAppAppDelegate : NSObject <UIApplicationDelegate> { 
     NSDictionary *arrayCount; 
    } 

    @property (nonatomic, assign) NSDictionary *arrayCount; 

    @end 

和.M

#import "SurveyAppAppDelegate.h" 
@implementation SurveyAppAppDelegate 
    @synthesize arrayCount; 

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
    { 


     arrayCount = [[NSDictionary alloc] init]; 

然后在应用程序委托的的dealloc我释放它

哦,并在NSMutableArray中的对象,我设置为injuredArray都是字符串

编辑:因此,我增加一个循环的NSLog在根视图控制器右阵列在我设置它之后,它不工作。所以它看起来出于某种原因,它没有被保存在arrayCount正确

感谢您的帮助

+0

在您的详细视图控制器中,surveyAppAppDelegate的引用来自哪里?你确定它不是空的? – picciano

+0

是的,我将它设置为一个NSDictionary,做属性和综合,然后我初始化它像这样arrayCount = [[NSDictionary alloc] init]; – Nick

+0

受伤阵列的价值是什么? –

回答

0

在你的appDelegate头,尽量保留了字典,而不是分配:

@property (nonatomic, retain) NSDictionary *arrayCount; 

然后执行:

NSDictionary *anArrayCount = [[NSDictionary alloc] init]; 
self.arrayCount = anArrayCount; 
[anArrayCount release]; 

的是你分配的受伤阵列对象已经分配并初始化了吗?这可能是问题...

+0

感谢您的答案,我认为问题是我把一个可变数组放入字典或东西,我不是100%肯定的,我只知道经过一些修补后现在可以工作 – Nick

+0

我后来遇到了一些问题,出现了相同类型的问题,它是通过保留字典来解决的,我的意思是保留它作为我通常情况下,一定是一个错字:/你不讨厌这些,因为当它是正确的语法,并没有提出一个错误信息,他们很难赶上 – Nick

0

你可能会考虑创建在您的详细视图控制器的init方法,它接受的NSDictionary,你有兴趣和保留在一个财产。我不能告诉其中来自即将在今年参考:surveyAppAppDelegate.arrayCount

+0

是的,我做到了这一点,你可以看看原来的帖子,我发布我的应用程序委托代码 – Nick

相关问题