Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Apr 17, 2011, 01:10 PM   #1
KiranPanesar
macrumors newbie
 
Join Date: Mar 2011
How would I access a label on another view controller?

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
Attached Files
File Type: zip TaskManager.zip (77.0 KB, 44 views)
KiranPanesar is offline   0 Reply With Quote
Old Apr 17, 2011, 01:14 PM   #2
dejo
Moderator
 
dejo's Avatar
 
Join Date: Sep 2004
Location: The Centennial State
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.
__________________
dejo is offline   0 Reply With Quote
Old Apr 18, 2011, 10:46 AM   #3
KiranPanesar
Thread Starter
macrumors newbie
 
Join Date: Mar 2011
Quote:
Originally Posted by dejo View Post
Learn more about the Model-View-Controller approach.
Will do. Can you recommend any online resources?
KiranPanesar is offline   0 Reply With Quote
Old Apr 18, 2011, 10:59 AM   #4
robbieduncan
Moderator
 
robbieduncan's Avatar
 
Join Date: Jul 2002
Location: London
Well, Apple provide this document as part of their "Cocoa Core Competencies". You should be familiar with everything in the table of contents.
robbieduncan is offline   0 Reply With Quote
Old Apr 18, 2011, 10:59 AM   #5
dejo
Moderator
 
dejo's Avatar
 
Join Date: Sep 2004
Location: The Centennial State
What are you currently using to learn iOS development?
__________________
dejo is offline   0 Reply With Quote
Old Apr 18, 2011, 11:24 AM   #6
KiranPanesar
Thread Starter
macrumors newbie
 
Join Date: Mar 2011
Quote:
Originally Posted by dejo View Post
What are you currently using to learn iOS development?
I'm using this. It has a section on MVC design, so I'll look at it again.
KiranPanesar is offline   0 Reply With Quote
Old Apr 18, 2011, 11:42 AM   #7
dejo
Moderator
 
dejo's Avatar
 
Join Date: Sep 2004
Location: The Centennial State
Quote:
Originally Posted by KiranPanesar View Post
I'm using this. It has a section on MVC design, so I'll look at it again.
This previous thread 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.
__________________
dejo is offline   0 Reply With Quote
Old Apr 18, 2011, 11:51 AM   #8
KiranPanesar
Thread Starter
macrumors newbie
 
Join Date: Mar 2011
Quote:
Originally Posted by dejo View Post
This previous thread 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?
KiranPanesar is offline   0 Reply With Quote
Old Apr 18, 2011, 11:53 AM   #9
robbieduncan
Moderator
 
robbieduncan's Avatar
 
Join Date: Jul 2002
Location: London
Quote:
Originally Posted by KiranPanesar View Post
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?
robbieduncan is offline   0 Reply With Quote
Old Apr 19, 2011, 03:18 AM   #10
jnoxx
macrumors 65816
 
jnoxx's Avatar
 
Join Date: Dec 2010
Location: Aartselaar // Antwerp // Belgium
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.
Code:
    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
jnoxx is offline   0 Reply With Quote
Old Apr 19, 2011, 09:40 AM   #11
dejo
Moderator
 
dejo's Avatar
 
Join Date: Sep 2004
Location: The Centennial State
Quote:
Originally Posted by jnoxx View Post
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.
Code:
    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
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:
Code:
    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.
__________________
dejo is offline   0 Reply With Quote
Old Apr 19, 2011, 10:15 AM   #12
jnoxx
macrumors 65816
 
jnoxx's Avatar
 
Join Date: Dec 2010
Location: Aartselaar // Antwerp // Belgium
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.
jnoxx is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Tags
multiple view controllers, objective-c, programming, xcode

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
thread Thread Starter Forum Replies Last Post
Resolved: Loading a Progmatically created view Controller LastLine iPhone/iPad Programming 3 Apr 27, 2011 09:25 PM
How do I access a label on a different view controller? KiranPanesar iPhone and iPod touch Apps 0 Apr 15, 2011 05:28 PM
How to get LightScribe Template Labeler on Mac? SETFREE2121 Mac Applications and Mac App Store 3 Feb 1, 2009 06:32 PM
Accessing sqlite3 database from multiple view controllers binjaja Mac Programming 1 Nov 17, 2008 10:46 PM
How do I put text labels on wallpapers? mac-convert Design and Graphics 10 May 20, 2008 04:00 AM


All times are GMT -5. The time now is 12:40 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC