0

I have two view controllers. In the first one I have a button and when it is clicked I want to show the other view controller in the right of the screen to login like modal present form sheet.

For the moment I have the second view controller with a label but when I click it appears empty, I don't know way because I put a label inside, and of course in the middle of the screen.

LoginViewController *loginController = [[LoginViewController alloc] init];
loginController.modalPresentationStyle = UIModalPresentationFormSheet;
[self presentViewController:loginController animated:YES completion:nil];
loginController.view.superview.frame = CGRectMake(0, 0, 300, 1000);
loginController.view.superview.center = self.view.center;

Any suggestion to see the label? To change the position?

Andrey Chernukha
  • 21,488
  • 17
  • 97
  • 161
Anna
  • 189
  • 1
  • 8
  • Try using `initWithNibName:@"MyNibName"]` for your `LoginViewController` initialization. It's probably creating a new instance of `UIViewController`, since it isn't linked to your nib file.. – Jeeter Jan 03 '13 at 15:20
  • I put the title and the storyboard id of this view controller with this name LoginViewController and then I change my code to LoginViewController *loginController = [[LoginViewController alloc] initWithNibName:@"LoginViewController" bundle:nil]; but an exception appears Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: – Anna Jan 03 '13 at 15:33
  • instead of `nil`, try `[NSBundle mainBundle]`, and see if that works. – Jeeter Jan 03 '13 at 15:34
  • 1
    http://stackoverflow.com/a/8853872/1523962 . Use the UIStoryboard instance method `instantiateViewControllerWithIdentifier:`. – John Sauer Jan 03 '13 at 15:40
  • Do you explicitly set the superview of your view in the LoginViewController class? Or anywhere else? Otherwise you should not try to change its properties. Since you add a new view controller, the new view will not be in the same view hierarchy as your other view. – Hjalmar Jan 03 '13 at 15:40
  • No, it doesn't work, the exception is the same – Anna Jan 03 '13 at 15:41
  • vaderkvarn, what do you mean? I have both view controllers independent one from each other because I thought than it should not be necessary because I want to show one on top to the other.. – Anna Jan 03 '13 at 15:50
  • Yes, that is reasonable, but why do you try to alter the properties of the superview? The superview of the view of a newly initialized view controller is usually something like a UITransitionView which is not part of the public API. – Hjalmar Jan 03 '13 at 15:54
  • Because it's the only solution that I found to modify the size of the view controller that I put in the top but not how I wanted it. – Anna Jan 03 '13 at 15:58
  • Any idea to change the position? I want to show on the right of the screen – Anna Jan 03 '13 at 16:06
  • Ok, I see. You should look for another solution though, as it is undocumented API. You are not supposed to alter that view. You should try to change the views properties directly instead, but do it before you present it, not after. – Hjalmar Jan 03 '13 at 16:08

1 Answers1

1

I think it would be best to use the custom container controller methods to do this. In the example code I have below, I made a login controller that was the size of a form sheet (540 x 600) and slid it in from the right onto the main view of ViewController so that it was centered vertically, and up against the right side.

In ViewController.m, I have this:

-(IBAction)showLogin:(id)sender {
    LoginController *login = [self.storyboard instantiateViewControllerWithIdentifier:@"Login"];
    login.view.frame = CGRectMake(768, 202, 540, 600);//centered vertically and offscreen to the right
    [self addChildViewController:login];
    [self.view addSubview:login.view];
    [UIView animateWithDuration:.5 animations:^{
        login.view.frame = CGRectMake(228, 202, 540, 600);
    } completion:^(BOOL finished) {
        [login didMoveToParentViewController:self];
    }];
}

And to remove the view, I have this in the LoginController.m:

-(IBAction)goBackToMain:(id)sender {
    [UIView animateWithDuration:.5 animations:^{
        self.view.frame = CGRectMake(768, 202, 540, 600);
    } completion:^(BOOL finished) {
        [self.view removeFromSuperview];
        [self removeFromParentViewController];
    }];
}

After Edit:

If you want to remove the login controller from a button in ViewController, you can do it like this:

-(IBAction)goBackToMain:(id)sender {
        LoginController *login = self.childViewControllers.lastObject;
        [UIView animateWithDuration:.5 animations:^{
            login.view.frame = CGRectMake(768, 202, 540, 600);
        } completion:^(BOOL finished) {
            [login.view removeFromSuperview];
            [login removeFromParentViewController];
        }];
    }
rdelmar
  • 103,982
  • 12
  • 207
  • 218
  • Great! This is better! :) And I would like to remove the login view if I click in the parent controller so I would have to put the goBackToMain in the ViewController in my case, would't I? – Anna Jan 04 '13 at 08:30
  • @Anna Yes, you could move that code to ViewController -- you can reference the login controller with self.childViewControllers.lastObject – rdelmar Jan 04 '13 at 16:42
  • Thank you. But I want that goBackToMain method be executed if a press in the parent view controller. If I have a specific button I know how to do it but not in all area. I don't know if I am explaining it correctly. The child view appear in the right part so If I click in the left are I want remove the child view controller. So what I have to do this action? Putting almost transparent image in the left part to catch click button... – Anna Jan 09 '13 at 12:45
  • I edited my answer to show how to use goBackToMain from ViewController. Is that you question, or are you asking about how to have one button do both the showing and removing of the login controller? – rdelmar Jan 09 '13 at 16:25
  • My question is if I can do goBackToMain action when I click in the parent area not only in a specific button. – Anna Jan 10 '13 at 08:24
  • @Anna, if you mean anywhere in the main view, then you need to add a gesture recognizer to that view, and make the goBackToMain method its action. – rdelmar Jan 10 '13 at 16:27
  • Ok! Thank you. I will look for it. :) – Anna Jan 11 '13 at 08:36