Load Uiviewcontroller

Asked

Viewed 89 times

0

I have a Uiviewcontroller with the black Background where would be my main Uiviewcontroller (where is the menu and etc) and I do this:

CredenciaisViewController *obj = [self.storyboard instantiateViewControllerWithIdentifier:@"Credencial"];    
[_viewContainer addSubview:obj.view];
    _viewContainer.alpha = 1;

Credentialviewcontroller has the red background and I carry it in a Uiview that is in my main Uiviewcontroller, but when it loads inside the view it turns half white and the Uibuttons when they are in the white part do not work.

Why does this happen? And how do I solve?

1 answer

2

You can’t add one UIViewController as subview other’s UIView. For that purpose, there is the Custom Container.

Then you can add through the method addChildViewController:childController in his UIContainerView and not UIView as in your example.

In the Builder interface you can locate him, so:

inserir a descrição da imagem aqui

Dynamically, do something like this in the action of your UIButton:

- (IBAction)mostrarCredenciais:(id)sender {
    CredenciaisViewController *credenciaisView = [self.storyboard instantiateViewControllerWithIdentifier:@"Credencial"];

    [self addChildViewController:credenciaisView];
    [credenciaisView didMoveToParentViewController:self];
    credenciaisView.view.frame = self.containerView.bounds;
    [self.containerView addSubview:credenciaisView.view];
}

With this you don’t need to do so the connection on storyboard of the container with the UIViewController. Just turn on the IBOutlet (which is a UIView normal), which in my example is the containerView.

  • When I drag Containerview to the screen, it creates a connection with another Uiviewcontroller, I need that connection or I can dynamically call the Uiviewcontroller I want?

  • I inserted Container View into my Uiviewcontroller that has the Menu. E connected it to: @Property(nonatomic,Weak)Iboutlet Uiview *viewContainer; When you click on the menu I tried to load the contents in 2 ways. First form: Credentialviewcontroller *obj = [self.storyboard instantiateViewControllerWithIdentifier:@"Credential"]; [_viewContainer addSubview:obj.view]; Second form: Credentialisviewcontroller *obj = [self.storyboard instantiateViewControllerWithIdentifier:@"Credential"]; [self addChildViewController:obj]; .

  • Why not make the connection on own storyboard since all the elements you need are there?

  • Paulo, really is better, I did the storyboard and pressed, but he loads as soon as I run the project and not when I click the button, how do I make it not open face? Sorry, I’m starting with Ios!

  • Okay, in this case it really has to be something dynamic. I edited my answer, see if you can play.

  • Paulo, I was able to reproduce yes and pressed the content, but when I click on the screen (outside of any button) with the loaded content it flashes the screen and loads the content again, duplicating it. Why?

  • I managed to stop duplicating the contents, I did this: Nsarray *viewsToRemove = [self.view subviews]; for (Uiview *v in viewsToRemove) { [v removeFromSuperview]; } And stopped duplicating when I click on the background of the screen where there is no button, but when I click on the screen outside of any button blink, I would just like to take it off, the rest is perfect.

  • This is a strange behavior that I did not find here, see the example very simple that I did. In no time had this blinking behavior.

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.