Obj-C加载自定义.plist文件-iPhone

Obj-C加载自定义.plist文件-iPhone,iphone,objective-c,xml,uitableview,Iphone,Objective C,Xml,Uitableview,我希望加载一个特定的.plist文件,具体取决于设置变量(area,在本例中)和用户按下的按钮。.plist的文件格式为area-buttonPressed.plist。下面是用于调用特定.plist文件的代码(当前仅取决于当前区域) 我的主要问题是,如果我将当前在视图中的代码显示在viewDidLoad中,如果我向后导航(我使用的是NavigationController),更改设置中的区域并再次移动到此viewController,则不会再次调用该代码。因此,在我第一次加载此视图时,此方法非

我希望加载一个特定的.plist文件,具体取决于设置变量(
area
,在本例中)和用户按下的按钮。.plist的文件格式为area-buttonPressed.plist。下面是用于调用特定.plist文件的代码(当前仅取决于当前区域)

我的主要问题是,如果我将当前在视图中的代码显示在viewDidLoad中,如果我向后导航(我使用的是NavigationController),更改设置中的区域并再次移动到此viewController,则不会再次调用该代码。因此,在我第一次加载此视图时,此方法非常有效,但在我再次构建文件之前,它将保持这种方式

我的主要问题是:

  • 设置以下类型的
    NSString*path=[[NSBundle mainBundle]路径的正确方法是什么:[NSString stringWithFormat:@“%@”,area]:;
    
    使用两个变量(设置变量区域和上一个视图中按下的按钮的值(我有一个NSString作为该值))
  • 如何在每次显示视图时更新*路径
  • 如果这是一个解决方案,我如何在按下“后退”按钮时卸载视图(这将解决问题,因为每次用户移动到此视图时都会调用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];
    }
    
    提前谢谢

    编辑: 关于我的NSLog所说的一些信息:

    每当我按下按钮加载表格视图时,我的控制台都会显示“viewWillAppear”和
    NSLog(@“Area is%@”,Area)显示正确的区域(本例中为区域1)


    NSLog(@“路径为%@”,路径)
    或者在选择Region1以外的任何内容时显示
    (null)
    (到目前为止,我只创建了Region1.plist),或者iPhone模拟器/4.2/../../…/Region1.plist

    我不太理解在
    视图中显示代码的问题:
    viewDidLoad
    在您的视图从XIB中未归档或在
    -loadView
    之后调用,具体取决于您的方法


    如果每次视图出现时都需要更新,则
    视图将出现:
    是放置此代码的正确位置。如果这样做失败,可能是因为NSUserDefaults没有时间在设置视图中更新和返回此视图之间进行同步。通常,同步大约每30秒进行一次,但可以使用
    [NSUserDefaults synchronize]

    强制同步。请注意,在使用NSUserDefaults时,您可以监听NSUserDefaultsDidChangeNotification,以在默认值发生更改时得到特别通知。我同意ViewWillExeen应该是执行此操作的正确位置。此外,NSUserDefaults是同步的(我使用的是InAppSettingsKit)。然而,到目前为止没有运气。有关NSLogs的更多信息,请参阅主帖子中的“我的编辑”。我仍然不清楚您遇到了什么具体问题,但您是否认为这可能是因为您试图加载一个不存在的.plist?因此,
    path
    将为零,
    dict
    将为零,导致
    self.names
    self.keys
    也为零。主要问题是,当我将设置更改为其他区域时,表中的数据没有更新。但是,我通过将[table reloadData]添加到我的ViewWillDisplay中解决了这个问题,这将重新加载表中的所有数据。现在,如果该区域没有可用的plist,它就不会显示任何行。谢谢你的帮助!
        - (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];
    }