2011-09-01 42 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

相關問題