X0r0N
X0r0N

Reputation: 1934

how do i add a UIView controller to a UIView

i have a custom class of type UIViewController. i want to add it to a UIView so that it can be used to output to the screen.

the UIView is called engineView my custom UIViewController is called Engine. there is a custom method in the controller called addImage.

the code is as follows:

Engine *engine = [[Engine alloc] init];
CGRect frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
engine.view.frame = frame;

UIImage *image = [UIImage imageNamed:@"photo2.jpg"];
[engine addImage:image];

[engineView addSubview: engine.view];

this does not display the image.

however if i add the view controller through storyboard, it seems to work (if i add the image at the viewdidlayoutsubviews method. but i would like to call functions to it programically from the parent UIViewCcontroller.

can anyone tell me how i do this?

Upvotes: 0

Views: 128

Answers (3)

Ravi
Ravi

Reputation: 2451

try like this..

Engine *engine = (Engine *)[self.storyboard instantiateViewControllerWithIdentifier:@"EngineViewControllerStoryBoardID"];

to set the story board id
enter image description here

to pass an image to Engine:

-->create a UIImage property in Engine.h

like @Property UIImage * engineImage;

--> assign that image to UIIMageView in Engine.m

-(void)viewWillAppear:(BOOL)animated
{ 


    engineImageView.image = engineImage; // engineImageView is an example UIImageView replace it with your UIImageView

}

--> now send the image like engine.engineImage =[UIImage imageNamed:@"photo2.jpg"];

Upvotes: 0

iCodes
iCodes

Reputation: 1457

enter image description here

You can set storyBoard identifier in interface builder and ca use it like suggested above.

  MyViewController *instance = [self.storyBoard instantiateViewControllerWithIdentifier:@"yourIdentifier"];

As far as adding a ViewController to UIView you can achieve that by using addChildViewControllermethod too.

Upvotes: 1

iOSDev
iOSDev

Reputation: 995

 try this..

     Engine *eng=[self.storyboardinstantiateViewControllerWithIdentifier:@"YourStoryboradId"];

*you can set storyboard id by going in Identity Inspector.

Setting StoryboardId After that You can add image on your View Controller by following code

        UIImageView *image=[UIImageView alloc]initWithFrame:CGRectMake(x, y, your width, your Height);
        img setImage:[UIImage imageNamed:@"imageName"];
        [eng addSubview:image];

Upvotes: 0

Related Questions