【发布时间】:2011-09-02 00:47:53
【问题描述】:
我在从可变字典中读取可变数组时遇到问题。我正在尝试将其读出,然后将其放入标签中。这是我的数据的组织方式。我有一个可变的字典数组。然后在我的表格视图中,我在索引路径中获取对象并将其设置为等于应用程序委托中的字典。然后在详细信息视图中,我加载从数组中取出的字典并从中读取并将值放入标签中。我的字典中的所有字符串都在工作,但数组不会读出,你能帮帮我吗
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];
}
在我的 App Delegate 中,它被设置为 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;
}
}
在我的 App Delegate 中,我像这样设置了 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];
然后在 app delegate 的 dealloc 中我释放它
哦,还有我设置为injuredArray的NSMutableArray中的对象都是字符串
编辑:所以我在设置后立即在根视图控制器中的 NSLog 数组中添加了一个循环,但它不起作用。所以它看起来由于某种原因它没有正确保存在 arrayCount 中
感谢您的帮助
【问题讨论】:
-
在您的详细视图控制器中,对surveyAppAppDelegate 的引用来自哪里?你确定它不为空吗?
-
是的,我将它设置为 NSDictionary,做属性和综合,然后我像这样初始化它 arrayCount = [[NSDictionary alloc] init];
-
受伤数组的值是多少?
标签: iphone objective-c nsmutablearray nsmutabledictionary