Reputation: 115
I have a navigation-based iOS application. In my RootViewController
, users see chapters from 1 to 18. When they select a cell, it takes them to the VersesViewController
, where they see another list from Verse 1 to whatever. Depending on what chapter they chose, they will see a different number of verses.
I would like to modify the contents of an NSArray
in the VersesViewController
when the user chooses a cell in the RootViewController
view. How can I do this?
Here is the RootViewController
code:
#import "RootViewController.h"
@implementation RootViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = @"Chapters";
chapterList = [[NSMutableArray alloc] init];
for (int i = 1; i<19; i++)
{
[chapterList addObject:[NSString stringWithFormat:@"Chapter %d", i]];
}
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [chapterList count];
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text = [chapterList objectAtIndex:indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
VersesViewController *detailViewController = [[VersesViewController alloc] initWithNibName:@"VersesViewController" bundle:nil];
if ([[chapterList objectAtIndex:indexPath.row] isEqual:@"Chapter 9"]) {
[detailViewController setNumberOfVerses:18];
} else {
[detailViewController setNumberOfVerses:72];
}
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
- (void)viewDidUnload
{
[super viewDidUnload];
self.title = nil;
}
- (void)dealloc
{
[super dealloc];
[chapterList release];
}
@end
Please note that I am a n00b at Obj-C so I will need very simple answers. Thanks.
Upvotes: 1
Views: 163
Reputation: 3836
You need more complex storage for your data. You can make array of dictionaries to describe all chapter's data.
- (void)viewDidLoad
{
chapterList = [[NSMutableArray alloc] init];
...
chapterInfo = [NSDictionary dictionaryWithObjectsAndKeys:@"Chapter 9", @"title", [NSNumber numberWithInt:18], @"numberOfVerses", nil];
[chapterList addObject:chapterInfo];
chapterInfo = [NSDictionary dictionaryWithObjectsAndKeys:@"Chapter 10", @"title", [NSNumber numberWithInt:72], @"numberOfVerses", nil];
[chapterList addObject:chapterInfo];
...
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
NSDictionary *chapterInfo = [chapterList objectAtIndex:indexPath.row];
cell.textLabel.text = [chapterInfo objectForKey:@"title"];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
VersesViewController *detailViewController = [[VersesViewController alloc] initWithNibName:@"VersesViewController" bundle:nil];
NSDictionary *chapterInfo = [chapterList objectAtIndex:indexPath.row];
[detailViewController setNumberOfVerses:[[chapterInfo objectForKey:@"numberOfVerses"] intValue]];
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];
}
The best would be to create plist file with description of all chapters and then load it like this:
NSString *path = [[NSBundle mainBundle] pathForResource:@"chapters" ofType:@"plist"];
NSArray *chaptersList = [[NSArray alloc] initWithContentsOfFile:path];
Upvotes: 2
Reputation: 21019
You should store the contents in a pList or some format. And then retrieve it according to which verse was pushed. Show us some code first so we can help you.
Upvotes: 2