PDA

View Full Version : How would I access a label on another view controller?




KiranPanesar
Apr 17, 2011, 01:10 PM
Hello everyone!

I'm in the process of learning iOS/iPhone development, and I've come across an issue.

I have a label in one view controller, I then have a text field on another view controller. I want the text of the label to read whatever is input into the text field.

I've attached a sample project and would really appreciate a hand. I think I've got the general gist (Create an object of the class of the second view controller inside the the first view controller's .h file to work with etc.) but I could really use the extra help.

Thanks a lot.

Regards,
Kiran



dejo
Apr 17, 2011, 01:14 PM
Learn more about the Model-View-Controller approach. A better way would be: both controllers should access the same piece of data from a common model object.

KiranPanesar
Apr 18, 2011, 10:46 AM
Learn more about the Model-View-Controller approach.

Will do. Can you recommend any online resources?

robbieduncan
Apr 18, 2011, 10:59 AM
Well, Apple provide this document (http://developer.apple.com/library/ios/#DOCUMENTATION/General/Conceptual/DevPedia-CocoaCore/MVC.html) as part of their "Cocoa Core Competencies". You should be familiar with everything in the table of contents.

dejo
Apr 18, 2011, 10:59 AM
What are you currently using to learn iOS development?

KiranPanesar
Apr 18, 2011, 11:24 AM
What are you currently using to learn iOS development?

I'm using this (http://www.amazon.co.uk/Yourself-IPhone-Application-Development-Yourself/dp/0672332205/ref=sr_1_1?ie=UTF8&s=books&qid=1303143817&sr=8-1). It has a section on MVC design, so I'll look at it again.

dejo
Apr 18, 2011, 11:42 AM
I'm using this (http://www.amazon.co.uk/Yourself-IPhone-Application-Development-Yourself/dp/0672332205/ref=sr_1_1?ie=UTF8&s=books&qid=1303143817&sr=8-1). It has a section on MVC design, so I'll look at it again.

This previous thread (http://forums.macrumors.com/showthread.php?t=1128011) brings up the same concern. I'm starting to wonder if that SAMS book doesn't really address the model part of the MVC paradigm properly.

KiranPanesar
Apr 18, 2011, 11:51 AM
This previous thread (http://forums.macrumors.com/showthread.php?t=1128011) brings up the same concern. I'm starting to wonder if that SAMS book doesn't really address the model part of the MVC paradigm properly.

He's used the same two books that I've learned iPhone development with. I'll still take a look back at Sams. But do you have any recommendations for a good explanation?

robbieduncan
Apr 18, 2011, 11:53 AM
He's used the same two books that I've learned iPhone development with. I'll still take a look back at Sams. But do you have any recommendations for a good explanation?

Did you open the link I posted above? If so, and it does not explain well enough, what did you not find clear?

jnoxx
Apr 19, 2011, 03:18 AM
Actually, i'm having butt loads of problems too. I have made huge projects, and sometimes it works like a charm, and somtimes confuses me like hell.
I'll give an example.
I have a RootView. which parses JSON, fills in the cells, and has some arrays also filling in the background. When u click on a cell, send it to the next page.
In my opinion I feel like doing hack and slash, because what I do, is set a string on my detailviewController, like this.
SpeakerDetailViewController *detailViewController = [[SpeakerDetailViewController alloc] initWithNibName:@"SpeakerDetailViewController" bundle:nil];
// ...
// Pass the selected object to the new view controller.
detailViewController.firstNameString = [speakerFirstName objectAtIndex:indexPath.row];
detailViewController.secondNameString = [speakerLastName objectAtIndex:indexPath.row];
detailViewController.bioString = [speakerBio objectAtIndex:indexPath.row];
detailViewController.speakerImageString = [speakerImageURL objectAtIndex:indexPath.row];
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];

so I set the strings, then set the labels on the viewWillAppear with the string, which feels like double work.
but detailViewController.secondNameLabel = [speakerLastName objectAtIndex:indexPath.row]; doesn't work.
Because same in another project. I have an imageview, and that loads an image, with NSData from an URL. that works great, but when I click it, I have a popup, which also loads this image, (supposed to). So i thought I just set the detailViewController.(imageviewname).image = blah, more code to set the image.

But this didn't work. So i thought I'd set the NSData to transferData, this also doesn't work. Pulling my hairs sometimes this objective C :p

dejo
Apr 19, 2011, 09:40 AM
Actually, i'm having butt loads of problems too. I have made huge projects, and sometimes it works like a charm, and somtimes confuses me like hell.
I'll give an example.
I have a RootView. which parses JSON, fills in the cells, and has some arrays also filling in the background. When u click on a cell, send it to the next page.
In my opinion I feel like doing hack and slash, because what I do, is set a string on my detailviewController, like this.
SpeakerDetailViewController *detailViewController = [[SpeakerDetailViewController alloc] initWithNibName:@"SpeakerDetailViewController" bundle:nil];
// ...
// Pass the selected object to the new view controller.
detailViewController.firstNameString = [speakerFirstName objectAtIndex:indexPath.row];
detailViewController.secondNameString = [speakerLastName objectAtIndex:indexPath.row];
detailViewController.bioString = [speakerBio objectAtIndex:indexPath.row];
detailViewController.speakerImageString = [speakerImageURL objectAtIndex:indexPath.row];
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];

so I set the strings, then set the labels on the viewWillAppear with the string, which feels like double work.
but detailViewController.secondNameLabel = [speakerLastName objectAtIndex:indexPath.row]; doesn't work.
Because same in another project. I have an imageview, and that loads an image, with NSData from an URL. that works great, but when I click it, I have a popup, which also loads this image, (supposed to). So i thought I just set the detailViewController.(imageviewname).image = blah, more code to set the image.

But this didn't work. So i thought I'd set the NSData to transferData, this also doesn't work. Pulling my hairs sometimes this objective C :p

Here's what I think would be a better approach. Rather than pass the individual components of that model object to the detailViewController, you just pass a reference to the object itself. Such as:
SpeakerDetailViewController *detailViewController = [[SpeakerDetailViewController alloc] initWithNibName:@"SpeakerDetailViewController" bundle:nil];
// Pass the selected object to the new view controller.
detailViewController.speaker = [speakers objectAtIndex:indexPath.row];
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];
speakers in this case is an array of Speaker objects, each of which contains properties for firstName, lastName, etc.

Note how that comment said "pass the selected object to the new view controller". That is what you should be doing.

jnoxx
Apr 19, 2011, 10:15 AM
Hey Dejo,

I went crazy over this. so we setted up a meeting with several objective C programmers, and they started teaching me more about the whole concept, for bigger projects, how to do this, and, using the SynthesizeSingleton class etc.
So thanks anyways, i'm getting quite close to my ending solution! :)
Appreciate it. Thanks.