Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dismissing modal view controller from app delegate

I am using the facebook SDK to login to my app. If the user is not logged in, the login VC modally appears. Once the user has tapped log in, it alerts the App Delegate if the login was successful. If it was, I want to dismiss the modal login VC. How do I do this from the app delegate?

like image 884
Alex Godbehere Avatar asked Dec 20 '22 17:12

Alex Godbehere


2 Answers

You could try dismissing the presented ViewController, as something has to present the modal view controller

UINavigationController *navigationController = (id) self.window.rootViewController;
[[navigationController presentedViewController] dismissModalViewControllerAnimated:NO];

If you would want to check if a specific ViewController is being presented (i.e. only dismiss when a certain one is shown) then you could add in a check.

UIViewController *viewController = [navigationController presentedViewController];

if ([viewController isMemberOfClass:[YourViewController class]]) {
    [viewController dismissModalViewControllerAnimated:NO];
}
like image 134
Oliver Atkinson Avatar answered Jan 07 '23 11:01

Oliver Atkinson


The appDelegate needs some way to know who the hosting viewController is, so it can send the dismiss message. You need to figure out some way to make this happen. One way would be to define an ivar on the appDelegate "callDismissOnMeIfFaceBookFails", and set it when you are in this situation, otherwise its nil.

Note if its nil, the appDelegate can send the dismiss message with no overhead no problems! Use nil messaging to your advantage (I use it all the time). [Aside: I see so much code "if(obj) [obj message];" Don't do the if - just send the message - if obj is nil it has no effect and is handled efficiently!]

EDIT:

So you have a class AppDelegate. In the #interface define a property:

@property (nonatomic, strong) UIViewController *callDismissOnMeIfFaceBookFails;

and in the implementation you @synthesize it. Define a method:

- (void)dismissLoginView
{
  [callDismissOnMeIfFaceBookFails dismissModalViewControllerAnimated:YES];
  callDismissOnMeIfFaceBookFails = nil; // don't need it now, this unretains it
}

So, before the modal view controller is presented, the presenting object sets the appDelegate property "callDismissOnMeIfFaceBookFails" to itself.

When the user has successfully logged in, the login object sends the message to appDelegate telling it to dismissLoginView.

like image 34
David H Avatar answered Jan 07 '23 09:01

David H