0

メインのviewControllerのファイルにNSMutableDictionarycalled*tempを作成し、このコードを追加してファイルから情報を取り込みました。.h.plist

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSString *path = [[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"];
    temp=[NSMutableDictionary dictionaryWithContentsOfFile:path];
}

同じビュー コントローラーで、ボタン アクションを追加し、次のコードを追加しました。

-(IBAction)mathButton:(UIButton *)_sender
{
    label1.text = [temp objectForKey:@"m1name"];
}

ここで、「label1 は のテキスト フィールドであり.xibm1nameのキーの 1 つです。.plist

しかし、実行すると機能せず、強調表示されlabel1.text = [temp objectForKey:@"m1name"];てアクセス不良と呼ばれます。

私はこれに数日間立ち往生しており、多くのことを試しました。答えは本当に役に立ちます。

ありがとう

4

2 に答える 2

0

.h:

@interface ...
{
    NSMutableDictionary* temp;
}

.m:

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSString* path = [[NSBundle mainBundle] pathForResource: @"Data"
                                                     ofType: @"plist"];

    BOOL exists = [[NSFileManager defaultManager] fileExistsAtPath: path];

    if (exists)
    {
        temp = [[NSMutableDictionary alloc] initWithContentsOfFile: path];
        NSLog(@"%@", [temp description]);
    }
}

- (IBAction) mathButton: (UIButton *)_sender
{
    label1.text = [temp objectForKey: @"m1name"];
}

MRC の場合:

- (void) dealloc
{
    [temp release];

    [super dealloc];
}
于 2013-05-29T00:37:43.440 に答える
0
temp=[NSMutableDictionary dictionaryWithContentsOfFile:path];

で作成した辞書を保持していませんdictionaryWithContentsOfFile:path。その行を次のように変更する必要があります。

temp = [[NSMutableDictionary dictionaryWithContentsOfFile:path] retain];

(および でリリースされていることを確認してくださいdealloc)、またはtempがプロパティの場合は、

self.temp = [NSMutableDictionary dictionaryWithContentsOfFile:path];
于 2013-05-28T23:30:24.617 に答える