[英]Obj-C Load Custom .plist file on load - iPhone
我想根據設置變量(在這種情況下為area
)以及用戶按下的按鈕加載特定的.plist文件。 .plist的文件格式為area-buttonPressed.plist。 以下是用於調用特定.plist文件的代碼(當前僅取決於當前區域)
- (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;
}
我的主要問題是,如果我將當前在viewWillAppear中的代碼放在viewDidLoad中,則如果我向后導航(我正在使用NavigationController),則不會再次調用它,請更改設置中的區域並移至該viewController再次。 結果,這種方法在我第一次加載此視圖時效果很好,但是在我再次構建文件之前,它將保持這種狀態。
我的主要問題:
NSString *path = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%@", area] ofType:@"plist"];
的正確方法是什么NSString *path = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%@", area] ofType:@"plist"];
使用2個變量(設置變量區域和在上一個視圖中按下的按鈕的值(該值我有一個NSString))? 以下是我的其余代碼:
- (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];
}
提前致謝!
編輯:關於我的NSLog的一些信息說:
每當我按下按鈕加載表格視圖時,控制台就會顯示“ viewWillAppear”和NSLog(@"Area is %@",area);
顯示正確的區域(在這種情況下為區域1)。
NSLog(@"Path is %@",path);
當選擇Region1以外的任何內容時,它會顯示(null)
(到目前為止,我只設置了Region1.plist),或者是iPhone Simulator / 4.2 /.../.../ Region1.plist
我不太了解將代碼包含在viewWillAppear:
。 從XIB取消存檔視圖之后或在-loadView
之后,視您的方法-loadView
定,將調用viewDidLoad
。
如果每次視圖出現時都需要更新,則viewWillAppear:
是放置此代碼的正確位置。 如果此操作不起作用,則可能是因為NSUserDefaults沒有時間在設置視圖中更新和推回該視圖之間進行同步。 通常,同步大約每30秒發生一次,但是可以通過[NSUserDefaults synchronize]
強制執行。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.