Note that there are some explanatory texts on larger screens.

plurals
  1. POObj-C Load Custom .plist file on load - iPhone
    primarykey
    data
    text
    <p>I'm looking to load a specific .plist file dependant on a settings variable (<code>area</code> in this case) and what button a user pressed. The file format for the .plist will be area-buttonPressed.plist. Below is the code that is used to call the specific .plist file (currently just dependant on the current area)</p> <pre><code>- (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; } </code></pre> <p>My main issue is that if I put the code that's currently in the viewWillAppear in the viewDidLoad, it won't get called again if I navigate back (I'm using a NavigationController), change the area in the settings and move to this viewController again. As a result, this method works great the first time I load this view, but it'll remain that way until I build my file again. </p> <p>My main questions:</p> <ol> <li>What is the correct way to set <code>NSString *path = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%@", area] ofType:@"plist"]; </code> using 2 variables (settings variable area and the value of the button pressed in the last view (I have a NSString for that value))?</li> <li>How do I update the *path every time the view is displayed?</li> <li>If this would be a solution, how do I unload the view whenever the 'Back' button is pressed (this would solve the issue, because viewDidLoad gets called every time a user moves to this view, right?)</li> </ol> <p>Below is the rest of my code:</p> <pre><code> - (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]; } </code></pre> <p>Thanks in advance!</p> <p>EDIT: Some info on what my NSLog's say:</p> <p>Whenever I press the button to load the table view, my console shows "viewWillAppear" and the <code>NSLog(@"Area is %@",area);</code> shows the correct area (Region1 in this case). </p> <p><code>NSLog(@"Path is %@",path);</code> either shows <code>(null)</code> when anything but Region1 is selected (I've only made Region1.plist so far), or iPhone Simulator/4.2/.../.../Region1.plist</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload