0

I'm trying to figure out the best way to pass strings between views.

Lets say, for example, the user can choose what flavor popsicle they want. In the opening view, they press a button which opens a table view controller that shows a table of 5 flavors. Lets say the user selects "Grape" and the view controller immediately closes and returns to the opening view. How do I get that string data of "Grape" to the opening view controller?

I've read a bit about NSUserDefaults and have played around with it a bit, but it seems a little wonky to me, and I'm not sure if that's the best bet. Suggestions?

1
  • Personally I see NSUserDefaults as an utility to store values that affect application behavior. You should look at @Flash84x's answers for the correct way to go. Commented Jun 18, 2011 at 2:15

5 Answers 5

3

There are a few ways to do this, you can post a notification that contains an object with the selected information which your opening view subscribes to. You would post this notification at the time that the option was selected in your case.

Another option is to use the delegate pattern, your opening view could be the delegate for your secondary view, when the option is selected a delegate method is called to pass that information back.

What you want to stay away from is providing your secondary view a reference to the opening view, this makes your view hierarchy rigid and your secondary view can no longer be re-used anywhere else without that same opening view.

To delve further into the delegate example which is the route I would take.

Define your ColorPickerViewController

@interface ColorPickerViewController

@property (nonatomic, assign) id delegate;
// other supporting properties

@end

@protocol ColorPickerViewControllerDelegate <NSObject>

@optional 
 - (void) colorWasPicked:(UIColor *)pickedColor;

@end

@implementation ColorPickerViewController

@synthesize delegate;

- (void)colorSelected { // some method that is called when the user selects a color
    if([self.delegate respondsToSelect(@selector(colorWasPicked:)]) {
        [self.delegate colorWasPicked:selectedColor];
    }
}

@end

And then your opening view controller would instantiate the ColorPickerViewController and assign itself as the delegate

@interface OpeningViewController : UIViewController <ColorPickerViewControllerDelegate> {


}


@implementation OpeningViewController 

// implement the delegate method you wrote

- (void) colorWasPicked:(UIColor *)pickedColor {

    self.userSelectedColor = pickedColor;

}

Obviously this is not cut and paste code but it should steer you in the right direction, there may be some mistakes but I think for the most part it should be clear.

Sign up to request clarification or add additional context in comments.

1 Comment

Figured it out. Thanks for sending me in the right direction. Took some further research, but it's doing what I want it to. Thanks again.
1

I can think of at least three reasonable approaches:

  1. The parent view controller retrieves the data it wants from the child view controller.
  2. The child view controller is given a delegate, which would typically be the parent, and calls some delegate method to deliver whatever information it has collected.
  3. The child posts a notification that tells anyone who cares to listen that its data has changed.

Most of the time, I prefer option #1. The parent view controller already knows something about the child view controller because, after all, it's the parent that instantiates the child. It's also simple for the parent to keep a reference to the child, and when the parent's view becomes visible it can easily grab the data that it needs.

Option #2 is good if you don't want the parent to have to keep track of the child -- it can instantiate the child, present it modally or push it onto the navigation stack, and then forget about it. It's also nice if you want some extra flexibility as to the recipient of the child's data. For example, you could make your data model the child's delegate rather than the parent view controller, and the chosen flavor (or whatever) could be delivered directly to the model.

Option #3 shines when there might be more than one object that's interested in changes to the child's data, or when you're not quite sure which object might be interested.

The defaults system is fine to use, particularly for small-ish data, but IMO it's better to think of it as a storage mechanism than as a communications channel.

Comments

0

In your example, you can use the UIPickerView to present the options to the user and get the selected values. Take a look at the doc for UIPickerView.

Comments

0

I think the real way to think of this is to conceptualize your problem in terms of Model, View, Controller, which is how you should approach all Cocoa based problems, as reiterated often by Apple's documentation.

Your two views, they are Views, they should only be concerned with interacting with the user (display information, take input).

There should be a Controller (plural?), this controller knows there are 2 views, and allows the views to interact with the data.. which is the Model.

The Model actually has a variable called NSString *flavor;. It holds that information. Any View, via interfacing with a Controller, gets that same variable flavor for display or modification.

To recap: You solve your problem by having the data in 1 place, the Model. A Controller exists to connect this data out to your multiple Views that have no idea where that actual data is behind held.

Comments

0

For this purpose, you can use NSNotifications.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.