How to Load a View in viewDidLoad?

2019-04-01 19:10发布

I want to load a second View at the beginning of a program. I thought, that the viewDidLoad-methode would be the right method. The problem is that it doesn't work.

The reason why I want to load a view in the viewDidLoad-method is that it is possible on a new device (iPad) to load a view over the other view.

How can I make it? I tried this, but it doesn't work:

- (void)viewDidLoad {
    StartViewController * start = [[StartViewController alloc]initWithNibName:nil bundle:nil];
    start.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    start.modalPresentationStyle = UIModalPresentationFormSheet;
    [self presentModalViewController:start animated:YES];
}

I tried addSubview and it works, but then I don´t have the nice transition. Any idea? I also tried awakeFromNib. Also this is not a question about the iPad, so I don't break the nda. It is a general question, how to load a new view in the viewDidLoad-method (or an other method).

2条回答
来,给爷笑一个
2楼-- · 2019-04-01 19:35

Try to use the following code

- (void)viewDidLoad {
    [super viewDidLoad];
    //I mean after above line
    if(![self isLogin]) {
    //to login the user
    [self gotoCredentials];
    }
}

-(void)gotoCredentials  {
    Login *objLoginViewController=[[Login alloc] initWithNibName:@"Login" bundle:nil];        
    UINavigationController *objnavigationController = [[UINavigationController alloc] 
                                                       initWithRootViewController:objLoginViewController];       
    objnavigationController.modalPresentationStyle=UIModalPresentationFormSheet;
    [self  presentModalViewController:objnavigationController animated:YES];
    [objLoginViewController release];
    objLoginViewController=nil;
    [objnavigationController release];
    objnavigationController=nil;
}

the above code works fine for me

查看更多
贪生不怕死
3楼-- · 2019-04-01 19:36

This works with viewDidAppear, not viewDidLoad. The view needs to have appeared for another to show up in front of it. Aside from that, I have the same code in some of my projects, does what you describe

查看更多
登录 后发表回答