1

I have two views, each with its own view controller. The first view has two buttons ("Button" and "Button2"). When I click on "Button", I load the second view controller, which contains a UIPickerView, which hovers over of first view (by performing addSubview) as is shown in the image below). When I click on the "Item" button of this second view, I hide the view with the UIPickerView. When I click on the "Item" button, I not only want to hide the view with the UIPickerView, but also to set name of button with item selected from UIPickerView.

(Each of these two view has its own view controller.)

screen snapshot

Rob
  • 415,655
  • 72
  • 787
  • 1,044
BERGUIGA Mohamed Amine
  • 6,094
  • 3
  • 40
  • 38
  • If you have two view controllers, one for that main view with the two buttons, and another for the view with the picker, then you'd generall pass data back using delegate pattern, as discussed in http://stackoverflow.com/questions/5210535/passing-data-between-view-controllers/9736559#9736559. If you did [the necessary custom container calls](http://developer.apple.com/library/ios/documentation/UIKit/Reference/UIViewController_Class/Reference/Reference.html#//apple_ref/doc/uid/TP40006926-CH3-SW81), you don't need an additional `delegate` property, but can reference `parentViewController`. – Rob Jun 28 '13 at 14:55
  • If, on the other hand, you only have one view controller, and you're just adding a `UIPickerView` to your view and then removing it, then The King's answer below should help. – Rob Jun 28 '13 at 14:55
  • i Have Two UiViewController. – BERGUIGA Mohamed Amine Jun 28 '13 at 14:59
  • Then (a) do the necessary containment calls; (b) your child will then have a `parentViewController` property that is set; (c) make the parent conform to some protocol that you'll use to pass the data back; and (d) use that protocol to let the child inform the parent of the data. – Rob Jun 28 '13 at 15:02

2 Answers2

2

The process is as follows:

  1. Define a protocol for the child view controller to inform the parent view controller:

    //
    //  ChildViewDelegate.h
    //
    
    #import <Foundation/Foundation.h>
    
    @protocol ChildViewDelegate <NSObject>
    
    - (void)didUpdateValueX:(NSString *)string;
    
    @end
    

    Obviously, replace didUpdateValueX with a more meaningful name.

  2. Define the parent view controller to conform to that protocol:

    //
    //  ViewController.h
    //
    
    #import <UIKit/UIKit.h>
    #import "ChildViewDelegate.h"
    
    @interface ViewController : UIViewController <ChildViewDelegate>
    
    // the rest of your interface here
    
    @end
    
  3. Make sure the parent controller implements the method from that protocol:

    - (void)didUpdateValueX:(NSString *)string
    {
        // do whatever you want with it
    }
    
  4. When the parent adds the child, make sure to call the necessary custom container calls, notably addChildViewController and didMoveToParentViewController:

    UIViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:@"child"];
    [self addChildViewController:controller];
    controller.view.frame = ...;
    [self.view addSubview:controller.view];
    [controller didMoveToParentViewController:self];
    
  5. When the child is ready to inform the parent and dismiss itself, it does something like:

    if ([self.parentViewController conformsToProtocol:@protocol(ChildViewDelegate)])
    {
        [(id<ChildViewDelegate>)self.parentViewController didUpdateValueX:someStringValue];
    
        [self willMoveToParentViewController:nil];
        [self.view removeFromSuperview];
        [self removeFromParentViewController];
    }
    else
    {
        NSLog(@"%s: %@ does not conform to ChildViewDelegate!!!", __FUNCTION__, self.parentViewController);
    }
    

    This calls the protocol method and then removes itself (calling the necessary containment methods, willMoveToParentViewController:nil and removeFromParentViewController).

Theoretically, you could simplify this (keep all the containment stuff, but abandon the protocol) if your parent had a class property, and the child could theoretically reference that directly, but best practice is to use a protocol, so child controllers are a little more agnostic about their parent controllers.

See Creating Custom Container View Controllers in the View Controller Programming Guide. For a discussion about why it's important to use these container calls in the first place, see the WWDC 2011 video Implementing UIViewController Containment.

Rob
  • 415,655
  • 72
  • 787
  • 1,044
0

You can access SELECTED Value from UIPickerView by using...

- (NSInteger)selectedRowInComponent:(NSInteger)component

Following I describe Basic Step For How to Get selected Value From UIPickerView.

NSInteger selctedRow;
selctedRow = [myPickerView selectedRowInComponent:0];
NSString *strValue = [myArrayOfPickerView objectAtIndex:selctedRow];

[myButtonName setTitle:strValue forState:UIControlStateNormal];

OR

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {

    NSString *strValue = [myArrayOfPickerView objectAtIndex:Row];
    [myButtonName setTitle:strValue forState:UIControlStateNormal];

}
  • But he's not just adding a picker to the view. If I read him correctly, he's got an entirely separate view controller for that contained view. So I suspect he's struggling on passing the data back to the `parentViewController`. – Rob Jun 28 '13 at 14:43
  • @Rob- OP only add UIView as Sub View OF ViewController and in subView he also added UIPickerView nothing anymore. –  Jun 28 '13 at 14:45
  • I read the question very differently from you (e.g. "I have two View (UiViewController)" and "each view has a controller"), but it's a little unclear, so you could well be right. – Rob Jun 28 '13 at 14:51
  • @Rob- first OP is not clear that have 2 view controller or UIView. if OP have 2 viewController then how it is possible to display two button (button and Button2) that are added in firstViewController ??? –  Jun 28 '13 at 14:55
  • i have two ViewController and i show the second viewController with [addSubView ...] – BERGUIGA Mohamed Amine Jun 28 '13 at 14:59
  • 1
    When doing [custom container view controllers](http://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/CreatingCustomContainerViewControllers/CreatingCustomContainerViewControllers.html#//apple_ref/doc/uid/TP40007457-CH18-SW6), it's frequently the case that the child view controller's views do not occupy the whole screen. If you're used to the "one controller for one screen", it's a little alien, but in complicated apps, custom containers are brilliant. Frankly, it's not clear if it's needed here, though. – Rob Jun 28 '13 at 14:59
  • @Rob- thanks for you expansive information i got new today :) really thanks :) –  Jun 28 '13 at 15:04