設定変数(この場合は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に再度移動すると、再び呼び出されることはありません。その結果、このメソッドは初めてこのビューをロードするときにはうまくいきますが、ファイルを再度ビルドするまではこの方法がそのまま残ります。
私の主な質問:
- ((私はその値のNSStringのを持っている)の設定変数領域と最後のビューで押したボタンの値)2つの変数を使用して
NSString *path = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%@", area] ofType:@"plist"];
を設定する正しい方法は何ですか? - ビューが表示されるたびに*パスを更新するにはどうすればよいですか?
- 解決策になる場合は、「戻る」ボタンを押すたびにビューをアンロードするにはどうすればいいですか?(ユーザーがこのビューに移動するたびに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
NSUserDefaultsを使用しているときは、デフォルトが変更されたときにNSUserDefaultsDidChangeNotificationが特に通知されるのを待つことができます。 – Anomie
私はviewWillAppearがこれを行うのに適切な場所であることに同意します。また、NSUserDefaultsは同期されています(私はInAppSettingsKitを使用しています)。しかし、これまでのところ運がない。 NSLogの詳細については、メイン・ポストの私の編集を参照してください。 – Moloch
私はまだあなたが持っている特定の問題についてはっきりしていませんが、存在しない.plistをロードしようとしているために可能性があると思いますか?したがって、 'path'はnilになり、' dict'はnilになり、 'self.names'となり、' self.keys'もnilになります。 –