Search code examples
iosobjective-cuiviewcontrollerpresentmodalviewcontroller

Modal UIViewController appears blank


I'm appending code to an existing app to add a modal loginView, the old project does not use xib, and the new view controller does.

The present project load its rootViewController this way

- (void) loadView
{
// this should take up the entire screen...
UIView * view = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
view.backgroundColor = [UIColor blueColor];
self.view = view;

sideBar =
    [[SideBarView alloc] init];
sideBar.rootViewController = self;
[self.view addSubview:sideBar];

pageView =
    [[PageView alloc] initWithTitle:DataEntryTitle
                             client:client];
pageView.rootViewController = self;
//I added this to instantiate the new view controller
_aLoginView = [[LoginViewController alloc]initWithUser:aUser];

[self.view addSubview:pageView];
}

To launch the modal view controller I'm using this code.

-(void)viewDidAppear:(BOOL)animated{

  _aLoginView.modalPresentationStyle = UIModalPresentationFormSheet;
  [self presentViewController:_aLoginView animated:YES completion:nil];
}

As long as I understand the reason for the modal LoginView appearing blank is that I'm creating a new instance of it, instead of calling the existing xib definition. Does someone know what should I do to reference the xib? The old project does not use storyboard, nor xib files.

Thanks,

With your info I think I'm doing something wrong in LoginViewController.m, but I don't know exactly what.

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
    // Custom initialization
}
return self;
}

- (id)initWithUser:(verifyUser *)aUser
{
self = [super initWithNibName:@"LoginViewController" bundle:[NSBundle mainBundle]];

if (self) {
    // Custom initialization
    _UserModel = aUser;
}
return self;
}

Solution

  • You want to create the view controller by loading it from the nib

    [[LoginViewController alloc] initWithNib:@"nib file name" bundle:[NSBundle mainBundle]]];
    

    If you want to keep your initWithUser: initializer you should be able to do

    self = [super initWithNib:@"nib file name" bundle:[NSBundle mainBundle]];