2011-03-02 8 views
1

設定変数(この場合はarea)とユーザーが押したボタンに応じて、特定の.plistファイルをロードしたいと考えています。 .plistのファイル形式は、area-buttonPressed.plistです。以下は、(現在のエリアで現在ちょうど依存)特定の.plistファイルを呼び出すために使用されるコードObj-Cロード時のカスタム.plistファイルのロード - iPhone

- (void)viewDidLoad { 
[super viewDidLoad]; 
NSLog(@"viewDidLoad"); 
self.navigationItem.title = @"Species List"; 

} 

-(void)viewWillAppear:(BOOL)animated{ 
    NSLog(@"viewWillAppear"); 
    area = [[NSUserDefaults standardUserDefaults] stringForKey:@"mulValue"]; 
    NSLog(@"Area is %@",area); 
    NSString *path = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%@", area] ofType:@"plist"]; 
    NSLog(@"Path is %@",path); 
    NSMutableDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:path]; 
    self.names = dict; 
    [dict release]; 
    NSArray *array = [[names allKeys] sortedArrayUsingSelector:@selector(compare:)]; 
    self.keys = array; 
} 

私の主な問題は、それは、私がのviewDidLoadでviewWillAppearで、現在のコードを置けばということではあり(私はNavigationControllerを使用しています)、設定内の領域を変更し、このviewControllerに再度移動すると、再び呼び出されることはありません。その結果、このメソッドは初めてこのビューをロードするときにはうまくいきますが、ファイルを再度ビルドするまではこの方法がそのまま残ります。

私の主な質問:

  1. ((私はその値のNSStringのを持っている)の設定変数領域と最後のビューで押したボタンの値)2つの変数を使用してNSString *path = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%@", area] ofType:@"plist"]; を設定する正しい方法は何ですか?
  2. ビューが表示されるたびに*パスを更新するにはどうすればよいですか?
  3. 解決策になる場合は、「戻る」ボタンを押すたびにビューをアンロードするにはどうすればいいですか?(ユーザーがこのビューに移動するたびにviewDidLoadが呼び出されるため、これで問題は解決します)
事前に

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return [keys count]; 
} 

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{ 
    NSString *key = [keys objectAtIndex:section]; 
    return key; 
    } 

// Customize the number of rows in the table view. 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    NSString *key = [keys objectAtIndex:section]; 
    NSArray *nameSection = [names objectForKey:key]; 
    return [nameSection count]; 
} 

// Customize the appearance of table view cells. 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    NSUInteger section = [indexPath section]; 
    NSUInteger row = [indexPath row]; 

    NSString *key = [keys objectAtIndex:section]; 
    NSArray *nameSection = [names objectForKey:key]; 

    static NSString *SectionsTableIdentifier = @"SectionsTableIdentifier"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SectionsTableIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SectionsTableIdentifier] autorelease]; 
    } 

    cell.textLabel.text = [nameSection objectAtIndex:row]; 
    return cell; 
    } 

- (void)viewDidUnload{ 
    self.names = nil; 
    self.keys = nil; 
    [super viewDidUnload]; 
} 

- (void)dealloc { 
    [names release]; 
    [keys release]; 
    [super dealloc]; 
} 

ありがとう:

以下は私のコードの残りの部分です!

編集:私はテーブルビューをロードするために、ボタンを押すたび

、私のコンソールは「viewWillAppear」を示し、NSLog(@"Area is %@",area);は正しい面積(この場合は地域1)を示しています。私のNSLogの言うことに いくつかの情報。

NSLog(@"Path is %@",path);地域1以外のものが選択されているときに(私はこれまでRegion1.plistを作りました)(null)を示し、またはiPhoneシミュレータのいずれか/ 4.2 /.../.../ Region1.plist

答えて

0

IドンコードがviewWillAppear:にあるという問題をかなり理解していません。 viewDidLoadは、あなたのアプローチに応じて、ビューがXIBから、または-loadViewの後にアーカイブ解除された後で呼び出されます。

ビューが表示されるたびに更新する必要がある場合は、viewWillAppear:がこのコードを挿入する正しい場所です。これがうまくいかない場合は、NSUserDefaultsが設定ビューで更新されてからこのビューに戻るまでに同期する時間がないためです。通常、同期は約30秒ごとに発生しますが、[NSUserDefaults synchronize]で強制的に実行できます。

+0

NSUserDefaultsを使用しているときは、デフォルトが変更されたときにNSUserDefaultsDidChangeNotificationが特に通知されるのを待つことができます。 – Anomie

+0

私はviewWillAppearがこれを行うのに適切な場所であることに同意します。また、NSUserDefaultsは同期されています(私はInAppSettingsKitを使用しています)。しかし、これまでのところ運がない。 NSLogの詳細については、メイン・ポストの私の編集を参照してください。 – Moloch

+0

私はまだあなたが持っている特定の問題についてはっきりしていませんが、存在しない.plistをロードしようとしているために可能性があると思いますか?したがって、 'path'はnilになり、' dict'はnilになり、 'self.names'となり、' self.keys'もnilになります。 –

関連する問題