How to add subview to another viewcontroller?-Collection of common programming errors

I’m doing an Ipad app. Now I have 2 viewcontrollers, ViewController has a button1 which has a popover segue to the second viewcontroller(PopoverController). Then, the PopoverController has a button2, if I click the button2, I’ll receive some UIImage from my server. I want to add fews subviews of UIImageView to the ViewController to display these images if I click the button2.

The button1 works well, the PopoverController can pop up as expected. BUT when I click the button2, nothing happend. I want to know how can I pass the data between 2 viewcontrollers and how to add subviews to another one.

Some codes relating to my problem:

ViewController.h:

#import 

@class PopoverController;

@interface ViewController : UIViewController
@property (strong, nonatomic) IBOutlet UIButton *button1;
@property (strong, nonatomic) PopoverController *popoverController;

@end

PopoverController.h:

#import 

@class ViewController;

@interface PopoverController : UIViewController
@property (strong, nonatomic) IBOutlet UIButton *button2;
@property (strong, nonatomic) UIImage *tempImg;
@property (strong, nonatomic) ViewController *viewController;

- (IBAction)addsubviews:(id)sender;

@end

I can not just use [viewController.view addSubview:img1]; in the - (IBAction)addsubviews:(id)sender;method to addsubview. So someone can help me? 🙂

====1st update====

Someone suggest that I have to use - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender method. I have tried this one by control click the button2 and create a custom segue between button2 and ViewController. When I clicked the button2, it showed : Terminating app due to uncaught exception 'NSGenericException', reason: 'Could not find a navigation controller for segue 'change'. Push segues can only be used when the source controller is managed by an instance of UINavigationController.'

So I’m wondering whether I should add a NavigationController. If so, what should I do?

====2nd update====

I use Paramasivan ‘s code now, and I found the way to call method from another viewcontroller. The problem now is the newly added subview in my viewcontroller doesn’t show up. I guess I have to update my viewcontroller in order to make it visible.

in my - (IBAction)addsubviews:(id)sender; method, i invoke the method in ViewController by [self.viewController createSubViewWithImage:_tempImg];

so the method can be invoked when i click the button2, but the view of viewcontroller has nothing changed.

  1. Add this in - (void)viewDidLoad,

    self.popoverController = [[PopoverController alloc] init]; 
    self.popoverController.viewController = self;
    

    Make sure that in no other places, you are setting self.popoverController = ....

    Do NOT add anything like self.viewController = ... in popovercontroller class. And you dont have to do self.viewController.popoverController = self; as well. Just remove these lines if you already have it.

    Once these are done, make sure that you are displaying self.popoverController only in the popover and you are not creating a new object for popoverController class there. So if these are fine, you can use any approach you want for passing the image from popoverController class to viewController class.

    as you mentioned in your comment you can use [self.viewController createSubViewWithImage:_tempImg]; in your popovercontroller class.

    Update: If you are doing via storyboard, you need to set this in prepareForSegue method and you dont have to create self.popoverController at all. Remove that part in your case. You can follow the procedure mentioned here to set up a custom segue and implement prepareForSegue method to pass the object. Source: On storyboards, views and passing data along

    1. Set the name of segue in storyboard to “CustomSegue”
    2. Implement prepareForSegue method
    3. Inside the method, check if name of segue matches “CustomSegue” and then set the viewController in the popoverController object there as,

    Try,

    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
        if ([segue.identifier isEqualToString:@"CustomSegue"]) {
            PopoverController *popoverController = [segue destinationViewController];
            popoverController.viewController = self;
        }
    }
    

    After doing this, you need to call [self.viewController createSubViewWithImage:_tempImg]; in your popoverController class.

  2. In ViewController.h add the following

    -(void)createSubViewWithImage:(UIImage *)imageDownloaded {
        UIImageView *imageViewTemp = [[UIImageView alloc] initWithImage:imageDownloaded];
        [self.view addSubView:imageViewTemp];
    }
    

    In PopoverController.h add the following

    @property (nonatomic, retain) ViewController *viewControllerPassed;
    

    And after image downloaded, call the following in PopoverController.h

    [viewControllerPassed createSubViewWithImage:imageDownloaded];
    

Originally posted 2013-11-15 09:08:52.