[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2985
  • Last Modified:

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
0
BYU-Studies
Asked:
BYU-Studies
  • 5
  • 3
1 Solution
 
pgnatyukCommented:
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
0
 
BYU-StudiesAuthor Commented:
Those are the two tutorials that I followed and they didn't work for me.
0
 
pgnatyukCommented:
They work for me. What is your question and with which one?
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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?

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

0
 
pgnatyukCommented:
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?
0
 
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.
0
 
pgnatyukCommented:
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
0
 
pgnatyukCommented:
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.


0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now