开发者

viewWillAppear and viewDidLoad for presenting login popup

开发者 https://www.devze.com 2023-03-09 07:47 出处:网络
I have a UIViewController in which it should pop up a LoginViewController if a user is not yet login. The question is where should I call this:

I have a UIViewController in which it should pop up a LoginViewController if a user is not yet login. The question is where should I call this:

LoginViewController* lvc = [[LoginViewController alloc] init];
    lvc.delegate = self;
    //[lvc setModalPresentationStyle:UIModalPresentationFullScreen];
    [self presentModalViewController:lvc animated:NO];
    [lvc release]; 

should it be in the viewDidLoad or in the viewWillAppear? I guess it makes sense to put it in the viewWillAppear? I tried to put it inside the viewDi开发者_StackOverflow社区dLoad and it gives me an extra border to the left and right of the view. Why is this?

UPDATE:

What I am trying to do here is to call presentModalViewController on the DetailViewController of a UISplitViewApplication. However nothing happens when I do so. I tried creating a new fresh project of a UISplitViewApplication and still it didn't work. The question is why? and how do I present a modal view in the viewWillAppear of a UISplitViewApplication


The modal window tries to initialize itself with respect to the view controller that called it (resizing the nib, for example). Creating and displaying it in its parent's viewDidLoad can sometimes give it wrong information since the parent is still itself loading. This is why you are seeing discrepancies. Presenting the modal controller in viewDidAppear is better in this case since all the parameters are ready to pass to the modal controller so it can load its own view properly. Though sometimes if you have a lot to load, even that isn't enough and you will need to wait longer before you can present your modal view (which doesn't sound like your case at all, so there should be nothing to worry about there). I hope this helps, though


I would place something like this in the AppDelegate.

- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
    [self.window addSubview:self.viewController.view];
    [self.window makeKeyAndVisible];    

    // Show the login screen if the user hasn't logged in yet
if (... login check here...)
  {
    LoginViewController* loginController = [[LoginViewController alloc] init];
    [self.viewController presentModalViewController:loginController animated:NO];
    [loginController release];
  }
}

Your login screen will be placed on top of your normal viewcontroller. After a succesfull login dismiss the LoginViewController and your user can start using your app.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号