Link to home
Start Free TrialLog in
Avatar of BYU-Studies
BYU-StudiesFlag for United States of America

asked on

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.


//JournalsTableViewController.h

- (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

JournalsTableViewController.png
JournalDetailViewController.png
Avatar of pgnatyuk
pgnatyuk
Flag of Israel image

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.

http://www.iphonesdkarticles.com/2009/01/uitableview-loading-detail-view.html
http://www.iphonesdkarticles.com/2009/03/drill-down-table-view-with-detail-view.html
http://hubpages.com/hub/IPhone-Guide-Loading-a-detail-view
Avatar of BYU-Studies

ASKER

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?
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?

OUTPUT

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=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"><soap:Body><journals xmlns=\"http://tempuri.org/\" /></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:@"http://byustudies.byu.edu/webservices/iPhone.asmx"]];
	[request setHTTPMethod:@"POST"];
	[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
	[request setValue:@"text/xml" forHTTPHeaderField:@"Content-Type"];
	[request setValue:@"byustudies.byu.edu" 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=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"><soap:Body><journalsResponse xmlns=\"http://tempuri.org/\"><journalsResult>" withString:@""];
	data = [data stringByReplacingOccurrencesOfString:@"</journalsResult></journalsResponse></soap:Body></soap:Envelope>" withString:@""];
	
	NSLog(data);
	
	// 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"];
				
		NSLog(title);
		NSLog(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?
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.

http://developer.apple.com/mac/library/documentation/cocoa/reference/foundation/Classes/NSDictionary_Class/Reference/Reference.html
ASKER CERTIFIED SOLUTION
Avatar of pgnatyuk
pgnatyuk
Flag of Israel image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial