Search code examples
iphoneuipickerview

How can I show the selected value from uipickerview to anotherview


Can anyone tell me how can I show the value from one a UIPickerView in another view? I am storing a value in a label but I have to show this value in a button in another view controller.


Solution

  • Typically you would store the value data in your model when the user closes the second view. and the first view would read the value from the model when it re-appears (or use notifications). Your model data could be a plist, or nsuserdefaults or core-data etc.

    Alternatively you could create a reference to the first viewController when you create your second viewController and assign this as a property on the second viewController. Then the second viewController in effect has a "path" to the first viewController:

    The first viewController would have a property such as:

    NSString *myStr;  // in the header
    @property (nonatomic, retain) NSString *myStr;  // in the header file
    @synthesize myStr;  // in the implementation file
    

    The second viewController would have a property such as:

    firstViewController *firstVC;  // in the header
    @property (nonatomic, retain) firstViewController *firstVC;  // in the header file
    @synthesize firstVC;  // in the implementation file
    

    When you create the second viewController you would do something like:

    secondViewController.firstVC = self;  // in the implementation file
    

    Then when you want to update myStr in the first ViewController (from the second viewController) you would do something like:

    firstVC = @" my new value ";  // in the implementation file