Passing Data Between Views

Discussion in 'iOS Programming' started by justfred, May 23, 2009.

  1. justfred macrumors newbie

    Joined:
    May 8, 2009
    #1
    What is the generally accepted convention for passing information between views?

    After studying different design patterns I'm leaning towards a Model-View-Controller. I am working on an application that uses a tab bar controller, four view controllers, four views, and potentially an object model or data model. I am thinking that some glue code in the four view controllers will connect the info and the display, or maybe simply a message sent between them somehow. I have subclassed UIView, UIViewController, and linked them all together with nibs for each tab.

    I have seen one example passing data through the app delegate, but this seemed to be regarded as the improper way to do it. Is there a better example, or a better way?

    I can definitely post the source code if you would like, but this question is more general. Essentially:

    User inputs data into a view. The data is processed 3 different ways, and output to 3 views that the use can access through the tab bar buttons. When information is updated, the app should update all four views.

    I haven't been able to find a clear example in the iphone SDK documentation. If any of you can keep me pointed in the right direction in my study I would greatly appreciate it! BTW This is my first post and I'm very glad to join your community.

    Thank you for your time,
    -Fred
     
  2. firewood macrumors 604

    Joined:
    Jul 29, 2003
    Location:
    Silicon Valley
    #2
    If you want to stick with MVC, sounds like you need one more object model or controller for app state (your 1 of 3 "ways"). Just have the views pass stuff through that object (or observe).
     
  3. North Bronson macrumors 6502

    Joined:
    Oct 31, 2007
    Location:
    San José
    #3
    I think this sounds like a good candidate for a Notification.

    I would make each view (or its view controller, if you are changing some underlying data in the corresponding model specific to that view) broadcast a Notification when some element was changed. That way, you can reach "across the hierarchy" (different tabs) without some kind of strong glue code.

    I would read the "Notifications" section in the "Communicating With Objects" chapter in the "Cocoa Fundamentals Guide".
     
  4. justfred thread starter macrumors newbie

    Joined:
    May 8, 2009
    #4
    Maybe.. but this app in particular is a very simple mathematic calculation, so as long as I can collect data from the model, I can probably return all 3 results from one model. If not then I will definitely add another and see how that goes. Thank you.

    great. yes this and remote messaging felt like a good place to study, but I wasn't sure. I think that for this very simple application, I could have a function in the 3 sub-views to receive a notification. Am I able to pass data through that notification?

    Thank you both for your help.
     
  5. mraheel macrumors regular

    Joined:
    Apr 18, 2009
    #5
    Yep, You can send data via the notification center, send your object class (it could be an array / string or anything) and receive it where ever you'd like.
     
  6. justfred thread starter macrumors newbie

    Joined:
    May 8, 2009
    #6
    thank you

    thank you. this is exactly what I wanted to achieve. I've got a few questions from what I've learned.

    I've built a simple application with a two views, one of which has a button to test the notification. For learning purposes the first view has a button to broadcast a notification to update. The second view has a button to register itself as an observer to the update notification.

    What is the best place to register a view as the observer when the program launches? Also, should the view receive the notification, or should the view controller be the observer and delegate to the view?

    I realize that both views have to be loaded for it to work. Should I load them when the information is updated, or load them at the launch of the program? Is it just a matter of allocating the other views when the main view loads, or something else?
     
  7. justfred thread starter macrumors newbie

    Joined:
    May 8, 2009
    #7
    an example?

    Does anyone know which sample code would be most applicable?

    I have succeeded at sending and receiving the notification, but I cannot seem to send the NSDictionary object with it.

    Am I missing something crucial?

    Thank you, Everyone!
     
  8. justfred thread starter macrumors newbie

    Joined:
    May 8, 2009
    #8
    This isn't the full program, of course, but this is essentially my implementation:

    In MainView.h and in AxialView.h
    Code:
    	
    NSMutableDictionary *values;
    NSString *helloKey;
    
    AxialView.h
    Code:
    NSString *helloFromMain;
    AxialView.m
    Code:
    
    - (void)update:(NSNotification *)notification
    {
    	helloFromMain = [values objectForKey:helloKey];
    	axialLabel.text = helloFromMain;
    }
    	
    //in an IBAction... later moved to viewDidLoad or whatever is most appropriate
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(update:) name:@"Update" object:values];
    
    MainView.m
    Code:
    //all of the below is also in an IBAction for testing purposes
    helloKey = @"helloKey";
    helloString = @"Hello String";
    
    values = [NSMutableDictionary dictionaryWithObject:helloString forKey:helloKey];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"Update" object:self userInfo:values];
    The notification worked before I added the dictionary, and now when the notification is broadcast, the string is updated like before, only now it is empty. I expected it to update to read "Hello String", on the Axial View when the button is pressed on the Main View.

    Thank you all for your help so far and in the future.
     
  9. justfred thread starter macrumors newbie

    Joined:
    May 8, 2009
    #9
    Nevermind, I figured it out after a few hours of searching.

    For anyone following along at home, the crucial thing I missed is the -userInfo method in the NSNotification class.

    Code:
    - (void)update:(NSNotification *)notification
    {
    	// key associated with string "Hello String"
    	helloKey = @"helloKey";
    
    	// [notificaiton userInfo] returns "values"
    	// [values objectForKey: helloKey] returns "Hello String"
    	helloFromMain = [[notification userInfo] objectForKey:helloKey]; 
    	axialLabel.text = helloFromMain;
    }
    Now off to fill the dictionary with an Array. Then I'm going to scrap the whole userInfo and store the values in SQLite... heh.
     

Share This Page