How would I access a label on another view controller?

Discussion in 'iPhone/iPad Programming' started by KiranPanesar, Apr 17, 2011.

  1. macrumors newbie

    Joined:
    Mar 25, 2011
    #1
    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:

  2. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    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.
     
  3. thread starter macrumors newbie

    Joined:
    Mar 25, 2011
    #3
    Will do. Can you recommend any online resources?
     
  4. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    Well, Apple provide this document as part of their "Cocoa Core Competencies". You should be familiar with everything in the table of contents.
     
  5. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #5
    What are you currently using to learn iOS development?
     
  6. thread starter macrumors newbie

    Joined:
    Mar 25, 2011
    #6
    I'm using this. It has a section on MVC design, so I'll look at it again.
     
  7. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #7
    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.
     
  8. thread starter macrumors newbie

    Joined:
    Mar 25, 2011
    #8
    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?
     
  9. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #9
    Did you open the link I posted above? If so, and it does not explain well enough, what did you not find clear?
     
  10. macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #10
    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 :p
     
  11. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #11
    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.
     
  12. macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #12
    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.
     

Share This Page