How do I pass data between views on the iPhone?

In my iPhone app, I have a list of Journal objects. Each journal has a title and titleID property. I am populating a table view with the list of journals and displaying the title for each journal in the view. When a user selects a journal, I need to pass that journal object to the next view so that I can access its properties (namely the titleID). How do I do this? I have tried a few tutorials with no luck.

I attached my didSelectRowAtIndexPath method below.


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	NSInteger row = [indexPath row];
	if (journalDetailViewController == nil) {
		self.journalDetailViewController = [[JournalDetailViewController alloc] initWithNibName:@"JournalDetailViewController" bundle:[NSBundle mainBundle]];
		[journalDetailViewController retain];
	selectedJournal = [journals objectAtIndex:row];
	journalDetailViewController.title = selectedJournal.title;
	BYUStudiesAppDelegate *delegate = [[UIApplication sharedApplication] delegate];
	[delegate.libraryNavController pushViewController:journalDetailViewController animated:YES];

Open in new window

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Probably, this journals objects was declared in a global namespace somewhere ( in a root controller), so you need to pass only the index of the selected journal. I'd like that way. But in all tutorials you will find a bit different way.
BYU-StudiesAuthor Commented:
Those are the two tutorials that I followed and they didn't work for me.
They work for me. What is your question and with which one?
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

BYU-StudiesAuthor Commented:
Ok, I got it work, but it's doing something really strange. I know it's really ghetto, but the way the web service is set up so that it sends back one XML tag and inside of it is the JSON that has the displayTitle and titleID for each journal. I just strip off the XML stuff and what's left is the JSON. As you can see from the code, when I print out all of the data, the JSON looks fine. However, when I create a dictionary and loop through each journal, the displayTitle works fine, but the titleID is different. Any ideas?


This is the output from "data" (I trimmed back some of the results):

[Session started at 2010-03-29 10:25:01 -0600.]
2010-03-29 10:25:06.562 BYUStudies[3377:207] {"Journals": [{"displayTitle":"Volume 48:4 (2009)", "titleID":8445},{"displayTitle":"Volume 48:3 (2009)", "titleID":8342},{"displayTitle":"Volume 48:2 (2009)", "titleID":8130},{"displayTitle":"Volume 48:1 (2009)", "titleID":7991},{"displayTitle":"Volume 47:4 (2008)", "titleID":7917}]}

This is the output when it loops through the list of journals (again, I only included a few results):

2010-03-29 10:25:06.566 BYUStudies[3377:207] Volume 48:4 (2009)
2010-03-29 10:25:06.567 BYUStudies[3377:207] 60076032
2010-03-29 10:25:06.568 BYUStudies[3377:207] Volume 48:3 (2009)
2010-03-29 10:25:06.568 BYUStudies[3377:207] 60159344
2010-03-29 10:25:06.569 BYUStudies[3377:207] Volume 48:2 (2009)
2010-03-29 10:25:06.569 BYUStudies[3377:207] 60159120
2010-03-29 10:25:06.570 BYUStudies[3377:207] Volume 48:1 (2009)
2010-03-29 10:25:06.571 BYUStudies[3377:207] 60154272
2010-03-29 10:25:06.572 BYUStudies[3377:207] Volume 47:4 (2008)
2010-03-29 10:25:06.572 BYUStudies[3377:207] 60069792

Notice how the titleID has changed from 8445 to 60076032? It's strange because if I don't convert the titleID to a string when it's looping through the journals, the app will crash and say the reason is because of NSDecimalNumber length. But everything should be a string anyways. Any help would be greatly appreciated! Thanks! :)

- (void)viewDidLoad {
    [super viewDidLoad];
	journals = [[NSMutableArray alloc] init];
	NSString *post =@"<?xml version=\"1.0\" encoding=\"utf-8\"?><soap:Envelope xmlns:xsi=\"\" xmlns:xsd=\"\" xmlns:soap=\"\"><soap:Body><journals xmlns=\"\" /></soap:Body></soap:Envelope>";
	NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
	NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
	NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
	[request setURL:[NSURL URLWithString:@""]];
	[request setHTTPMethod:@"POST"];
	[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
	[request setValue:@"text/xml" forHTTPHeaderField:@"Content-Type"];
	[request setValue:@"" forHTTPHeaderField:@"Host"];
	[request setHTTPBody:postData];
	[request addValue:@"2" forHTTPHeaderField: @"ID"];
	NSError *error;
	NSURLResponse *response;
	NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
	NSString *data=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
	data = [data stringByReplacingOccurrencesOfString:@"<?xml version=\"1.0\" encoding=\"utf-8\"?><soap:Envelope xmlns:soap=\"\" xmlns:xsi=\"\" xmlns:xsd=\"\"><soap:Body><journalsResponse xmlns=\"\"><journalsResult>" withString:@""];
	data = [data stringByReplacingOccurrencesOfString:@"</journalsResult></journalsResponse></soap:Body></soap:Envelope>" withString:@""];
	// Create a dictionary from the JSON string
	NSDictionary *results = [data JSONValue];
	// Build an array from the dictionary for easy access to each entry
	NSMutableArray *lstJournals = [results objectForKey:@"Journals"];
	// Loop through each entry in the dictionary...
	for (NSDictionary *journal in lstJournals)
		// Get title of the journal
		NSString *title = [journal objectForKey:@"displayTitle"];
		NSString *titleID = [journal objectForKey:@"titleID"];
		Journal *aJournal = [[Journal alloc] initWithTitle:title titleID:titleID];
		[journals addObject:aJournal];
	// Update the table with data
	[journalTableView reloadData];

Open in new window

It is like totally another story.
I see the input data. You take the title ID as a string. Sorry, I do not understand much here, you don't think that this is a number? NSNumber?
BYU-StudiesAuthor Commented:
It shouldn't be. All of the JSON comes in from the database as one big long string. It works if I save the values to a string array, but if I save them as a property in the Journal object it freaks out.
So check in the debug mode.
Seems like you are right and everything should work. These values from titleID look familiar.
So the title object is okay, but titleID that's supposed to be NSString too is wrong.

There is objectEnumerator and keyEnumerator in NSDictionary.
I got the same strange numbers in the log today. I remembered that I've seen this problem.
      NSNumber *n = [self valueForKey:@"fido"];
      NSLog(@"fido = %d", n);
In this code above I should write:
      NSLog(@"fido = %@", n);
If I want to see the correct value. So in your code you can also try:
NSLog(@"titleID=%@", titleID);
I think, you'll see the correct value.
You can make a short test:
NSString *str = [[NSString alloc] initWithFormat:@"%d", 123];
NSNumber *num = [NSNumber numberWithInt:[str intValue]];
NSLog(@"%@, %@, %d", str, num, num);

For line like NSLog(str); the compiler will give you a warning.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.