{"id":1396,"date":"2022-08-30T15:16:11","date_gmt":"2022-08-30T15:16:11","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/15\/how-to-add-subview-to-another-viewcontroller-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:16:11","modified_gmt":"2022-08-30T15:16:11","slug":"how-to-add-subview-to-another-viewcontroller-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/how-to-add-subview-to-another-viewcontroller-collection-of-common-programming-errors\/","title":{"rendered":"How to add subview to another viewcontroller?-Collection of common programming errors"},"content":{"rendered":"<p>I&#8217;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&#8217;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.<\/p>\n<p>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.<\/p>\n<p>Some codes relating to my problem:<\/p>\n<p>ViewController.h:<\/p>\n<pre><code>#import \n\n@class PopoverController;\n\n@interface ViewController : UIViewController\n@property (strong, nonatomic) IBOutlet UIButton *button1;\n@property (strong, nonatomic) PopoverController *popoverController;\n\n@end\n<\/code><\/pre>\n<p>PopoverController.h:<\/p>\n<pre><code>#import \n\n@class ViewController;\n\n@interface PopoverController : UIViewController\n@property (strong, nonatomic) IBOutlet UIButton *button2;\n@property (strong, nonatomic) UIImage *tempImg;\n@property (strong, nonatomic) ViewController *viewController;\n\n- (IBAction)addsubviews:(id)sender;\n\n@end\n<\/code><\/pre>\n<p>I can not just use <code>[viewController.view addSubview:img1];<\/code> in the <code>- (IBAction)addsubviews:(id)sender;<\/code>method to addsubview. So someone can help me? \ud83d\ude42<\/p>\n<p>====1st update====<\/p>\n<p>Someone suggest that I have to use <code>- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender<\/code> 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 : <code>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.'<\/code><\/p>\n<p>So I&#8217;m wondering whether I should add a <code>NavigationController<\/code>. If so, what should I do?<\/p>\n<p>====2nd update====<\/p>\n<p>I use Paramasivan &#8216;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&#8217;t show up. I guess I have to update my viewcontroller in order to make it visible.<\/p>\n<p>in my <code>- (IBAction)addsubviews:(id)sender;<\/code> method, i invoke the method in ViewController by <code>[self.viewController createSubViewWithImage:_tempImg];<\/code><\/p>\n<p>so the method can be invoked when i click the button2, but the view of viewcontroller has nothing changed.<\/p>\n<ol>\n<li>\n<p>Add this in <code>- (void)viewDidLoad<\/code>,<\/p>\n<pre><code>self.popoverController = [[PopoverController alloc] init]; \nself.popoverController.viewController = self;\n<\/code><\/pre>\n<p>Make sure that in no other places, you are setting <code>self.popoverController = ...<\/code>.<\/p>\n<p>Do <strong><em>NOT<\/em><\/strong> add anything like <code>self.viewController = ...<\/code> in <code>popovercontroller<\/code> class. And you dont have to do <code>self.viewController.popoverController = self;<\/code> as well. Just remove these lines if you already have it.<\/p>\n<p>Once these are done, make sure that you are displaying <code>self.popoverController<\/code> only in the popover and you are not creating a new object for <code>popoverController<\/code> class there. So if these are fine, you can use any approach you want for passing the image from <code>popoverController<\/code> class to <code>viewController<\/code> class.<\/p>\n<p>as you mentioned in your comment you can use <code>[self.viewController createSubViewWithImage:_tempImg];<\/code> in your <code>popovercontroller<\/code> class.<\/p>\n<p><strong>Update:<\/strong> If you are doing via storyboard, you need to set this in <code>prepareForSegue<\/code> method and you dont have to create <code>self.popoverController<\/code> at all. Remove that part in your case. You can follow the procedure mentioned here to set up a custom segue and implement <code>prepareForSegue<\/code> method to pass the object. Source: On storyboards, views and passing data along<\/p>\n<ol>\n<li>Set the name of segue in storyboard to &#8220;CustomSegue&#8221;<\/li>\n<li>Implement <code>prepareForSegue<\/code> method<\/li>\n<li>Inside the method, check if name of segue matches &#8220;CustomSegue&#8221; and then set the <code>viewController<\/code> in the <code>popoverController<\/code> object there as,<\/li>\n<\/ol>\n<p>Try,<\/p>\n<pre><code>- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {\n    if ([segue.identifier isEqualToString:@\"CustomSegue\"]) {\n        PopoverController *popoverController = [segue destinationViewController];\n        popoverController.viewController = self;\n    }\n}\n<\/code><\/pre>\n<p>After doing this, you need to call <code>[self.viewController createSubViewWithImage:_tempImg];<\/code> in your <code>popoverController<\/code> class.<\/p>\n<\/li>\n<li>\n<p>In ViewController.h add the following<\/p>\n<pre><code>-(void)createSubViewWithImage:(UIImage *)imageDownloaded {\n    UIImageView *imageViewTemp = [[UIImageView alloc] initWithImage:imageDownloaded];\n    [self.view addSubView:imageViewTemp];\n}\n<\/code><\/pre>\n<p>In PopoverController.h add the following<\/p>\n<pre><code>@property (nonatomic, retain) ViewController *viewControllerPassed;\n<\/code><\/pre>\n<p>And after image downloaded, call the following in PopoverController.h<\/p>\n<pre><code>[viewControllerPassed createSubViewWithImage:imageDownloaded];\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-15 09:08:52. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I&#8217;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&#8217;ll receive some UIImage from my server. I want to add fews subviews of UIImageView to the ViewController to display these [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1396","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1396","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=1396"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1396\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1396"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1396"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1396"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}