dismissViewControllerAnimated issue - ios

The first screen of my app consists of the login screen. I am displaying the login screen from AppDelegate.
The login screen comes up and I am able to go to next screen in the simulator. But it doesn't work on device.
On device, the main screen comes up for a second and the login screen is displayed. I tried changing the order of [self.window.rootViewController dismissViewControllerAnimated:YES completion:nil]; and [self.window.rootViewController presentViewController:self.navigationController animated:YES completion:nil]; but that doesn't work either.
I am running on a iOS 9.0 iPad (device). Everything seems fine on simulator though.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = nil;
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self->_loginViewController = [[LoginViewController alloc] initWithNibName:nil bundle:nil];
self.window.rootViewController = nil;
self.window.rootViewController = self->_loginViewController;
[self.window makeKeyAndVisible];
return YES;
}
- (void)loginUser {
if([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad) {
//load xib for iPad
mainVC = [[MainVC alloc] initWithNibName:#"MainVC~ipad" bundle:nil];
} else if([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPhone) {
//load xib for iPhone
mainVC = [[MainVC alloc] initWithNibName:#"MainVC~iphone" bundle:nil];
}
self.navigationController = nil;
self.navigationController = [[UINavigationController alloc] initWithRootViewController:mainVC];
dispatch_async(dispatch_get_main_queue(),^ {
// [self.window.rootViewController.navigationController popViewControllerAnimated:YES]; //not working
[self.window.rootViewController dismissViewControllerAnimated:YES completion:nil]; //not working
[self.window.rootViewController presentViewController:self.navigationController animated:YES completion:nil];
//not working
// [self.window.rootViewController.presentingViewController dismissViewControllerAnimated:YES completion:^{
// [self.window.rootViewController presentViewController:self.navigationController animated:YES completion:nil];
// }];
});
}

First thing is you are setting up LoginViewController as the rootController of the window which is fine. In your login method you are trying to dismiss a controller when none is presented here(assuming you have not presented any other controller). You can remove that line and just present the navigation controller.

For this purpose, I normally change my rootViewController. In your case, when your user has logged on replace:
[self.window.rootViewController dismissViewControllerAnimated:YES completion:nil]; //not working
[self.window.rootViewController presentViewController:self.navigationController animated:YES completion:nil];
with:
self.window.rootViewController = self.navigationController;
And then on user logout do:
self.window.rootViewController = self.loginViewController;

Related

Fix Warning: Attempt to present ViewController on NavigationController whose view is not in the window hierarchy

Currently, I have set up a log-in view for users of my app. Below is the code that presents this log-in view to the user:
// Handle how we present the view
if (self.notificationToProcess != nil) {
[self.navigationController dismissViewControllerAnimated:YES completion:^{
SWNotificationsViewController *viewController = [[NotificationsViewController alloc] init];
viewController.initialDataID = self.notificationToProcess[#"Data"];
self.notificationToProcess = nil;
[self.navigationController pushViewController:viewController animated:YES];
}];
} else if (self.detailURL != nil) {
[self.navigationController dismissViewControllerAnimated:YES completion:^{
WebViewController *wvc = [[WebViewController alloc] init];
wvc.URL = self.detailURL;
wvc.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self.navigationController presentViewController:wvc animated:YES completion:nil];
self.detailURL = nil;
}];
} else {
[self.navigationController dismissViewControllerAnimated:YES completion:nil];
}
What I'm attempting to do now is display a web-view first if the user has just updated the app. Below is what this new code looks like:
// Handle how we present the view.
if (self.notificationToProcess != nil) {
[self.navigationController dismissViewControllerAnimated:YES completion:^{
SWNotificationsViewController *viewController = [[SWNotificationsViewController alloc] init];
viewController.initialDataID = self.notificationToProcess[#"Data"];
self.notificationToProcess = nil;
[self.navigationController pushViewController:viewController animated:YES];
}];
} else if (self.detailURL != nil) {
[self.navigationController dismissViewControllerAnimated:YES completion:^{
SWWebViewController *wvc = [[SWWebViewController alloc] init];
wvc.URL = self.detailURL;
wvc.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self.navigationController presentViewController:wvc animated:YES completion:nil];
self.detailURL = nil;
}];
else if (![versionOfLastRun isEqual:currentVersion])
{
SWWebViewController *webViewController = [[SWWebViewController alloc] init];
NSString *url=#"http://someurl";
webViewController.URL = url;
webViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self.navigationController presentViewController:webViewController animated:YES completion:nil];
[[NSUserDefaults standardUserDefaults] setObject:currentVersion forKey:#"VersionOfLastRun"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
else if (versionOfLastRun == nil){
// First start after installing the app
}
else {
[self.navigationController dismissViewControllerAnimated:YES completion:nil];
}
However, when I run the app, the web view is not displayed, I get a warning of:
Warning: Attempt to present ViewController on NavigationController whose view is not in the window hierarchy!
Can anyone help me diagnose the problem? Thank you!
Seems like your viewController is not in Navigation controller hierarchy. This means that your controller is not connected with navigation controller stack which should handle presented controllers.
The other thing that should help is setting your root view controller as follows:
self.window.rootViewController = self.ViewController;
EDIT: since you mention that you don't use storyboards, setting rootViewController should do the trick.
Try to set the window's rootViewController to the navigation controller after you initialize the navigation controller:
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:mainController];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = navController;
This is how I'm doing it without storyboards.
The problem was that I didn't call self.navigationController dismissViewControllerAnimated:YES completion:^
After correcting this (see listing below), my WebView was shown as expected.
else if (![versionOfLastRun isEqual:currentVersion])
{
[self.navigationController dismissViewControllerAnimated:YES completion:^{
SWWebViewController *webViewController = [[SWWebViewController alloc] init];
NSLog(#"%#", self.window.rootViewController);
NSString *url=#"http://devstatwatch.drbsystems.com/releases_mobile.php";
webViewController.URL = url;
webViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self.navigationController presentViewController:webViewController animated:YES completion:nil];
[[NSUserDefaults standardUserDefaults] setObject:currentVersion forKey:#"VersionOfLastRun"];
[[NSUserDefaults standardUserDefaults] synchronize];
}];
}

How to return to view controller in main.storyboard from a xib without using navigation controller or navigation bar?

I am making an app with different view controllers. My home screen is in main.storyboard. Rest of the view controllers have their own xib, .h & .m files. I am trying this for navigation from home screen.
-(IBAction)btnSignUpTapped:(id)sender
{
SignUpWithEmailViewController * login = [[SignUpWithEmailViewController alloc] init];
UINavigationController *nav = [[UINavigationController alloc]
initWithRootViewController:login];
[self presentViewController:nav animated:YES completion:NULL];
}
This code works fine for navigating to different viewcontroller (in this case SignUpwithEmailViewController). On SignUpWithEmailViewController I have a back button which is supposed to bring me back to home screen. This is what I got so far:
-(IBAction)btnBackTapped:(id)sender
{
ViewController * homeScreen = [[ViewController alloc] init];
UINavigationController *nav = [[UINavigationController alloc]
initWithRootViewController:homeScreen];
[self presentViewController:nav animated:YES completion:NULL];
}
But as result of this code, screen turns black and nothing happens. How do I solve this problem? For hiding the nav bar I am using
-(void) viewWillAppear:(BOOL)animated
{
[[self navigationController] setNavigationBarHidden:YES animated:NO];
}
Yes, you have to insert you called viewController to NavigationController,
but if you wont to use modal flow - to close presentedViewController just call:
[self dismissViewControllerAnimated:YES completion:^{
}];
EDITED:
may be it will be more helpful, it just example, put it to you main view controller:
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
UIViewController *c = [[UIViewController alloc] init];
c.view.backgroundColor = [UIColor redColor];
[self presentViewController:c animated:YES completion:^{
[self dismissViewControllerAnimated:YES completion:^{
}];
}];
}

Presenting ViewController from appDelegate

I need to show a ViewController from the appDelegate every time the app comes from the background in the applicationDidEnterBackground method.
The *securityCheck prompts the user for a passcode very much like the normal passcode in iOS. Once the passcode is validated I call dimissViewControllerAnimated inside the securityCheck and I am left with my blank UINavigationController, since the view was presented from the appDelegate I have no record of who presented the view, so I can't popToRootViewController.
My question is how can I properly dismiss the SecurityCheckViewController so that it shows the ViewController which the user was on before the app entered the background.
Here's my code:
This method is called inside AppDelegate.m
- (void)securityCheck {
SecurityCheckViewController *securityCheck = [[SecurityCheckViewController alloc] init];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:securityCheck];
[securityCheck presentedByAppDelegate:YES];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self.window setRootViewController:navigationController];
[self.window makeKeyAndVisible];
}
Then inside the SecurityCheckViewController.m I have
- (void)unlockWasSuccessfulForPadLockScreenViewController:(ABPadLockScreenViewController *)padLockScreenViewController {
[self.appDelegate securityCheckDone];
[padLockScreenViewController dismissViewControllerAnimated:YES completion:nil];
NSLog(#"Sucsessfull Unlock");I'm
}
Present
- (void)securityCheck {
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main_iPhone" bundle:nil];
SecurityCheckViewController *securityCheck = [storyboard instantiateViewControllerWithIdentifier:#"securityCheckView"];
[self.window.rootViewController presentViewController:securityCheck animated:NO completion:nil];
}
Dismiss
[self dismissViewControllerAnimated:YES completion:nil];
You can call this from application:didFinishLaunchingWithOptions
NOTE: All the storyBoard "initialViewControllers" tick box on the property inspector is turned off
UIStoryboard *storyboard =
[UIStoryboard storyboardWithName:#"Registration" bundle:nil];
RegisterViewController *vc = [storyboard instantiateViewControllerWithIdentifier:#"RegisterViewController"];
//this is key else you get a black screen
self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];
self.window.rootViewController = vc;
[self.window makeKeyAndVisible];

Presenting a ViewController breaks the previous ViewController

My problem is when I present a UIViewController the presenting views are going black.
I have a UIViewController named mainViewController which is the root view of my windows.
Inside I have a MMDrawerController (just added as a subview of mainViewController's view).
The MMDrawerController contains the rest of my views.
But when I'm presenting a new UIViewController from my mainViewController, the new VC displays well, but when it dismisses it left only black screen behind.
To note the black screen appears when adding (I can see it directly). Not when dismissing.
For testing purpose I did this code :
UIViewController *vc = [UIViewController new];
vc.view.backgroundColor = [UIColor redColor];
[[[[UIApplication sharedApplication] keyWindow] rootViewController] presentViewController:vc animated:NO completion:^{
[vc dismissViewControllerAnimated:YES completion:nil];
}];
Which do the same black result as normal use. (normally it's a QLViewController...)
My mainViewController is set like it :
_mainViewController = [MyMainViewController new];
_window.rootViewController = _mainViewController;
[self.window addSubview:_mainViewController.view];
Souce code of MMDrawerController which is up to date in my project
I have tested the code just in MMDrawerController example project, but I can't reproduce the problem, following is what I have tried:
MMAppDelegate.m
-(BOOL)application:(UIApplication *)application willFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
//...
UIViewController *testVc = [[UIViewController alloc] init];
testVc.view.backgroundColor = [UIColor greenColor];
[testVc.view addSubview:self.drawerController.view];
[self.window setRootViewController:testVc];
[self.window addSubview:testVc.view];
return YES;
}
MMExampleSideDrawerViewController.m:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
switch (indexPath.section) {
case MMDrawerSectionViewSelection:{
UIViewController *vc = [UIViewController new];
vc.view.backgroundColor = [UIColor redColor];
UIViewController *mainVC = [[UIApplication sharedApplication] keyWindow].rootViewController;
[mainVC presentViewController:vc animated:YES completion:^{
[vc dismissViewControllerAnimated:YES completion:nil];
}];
return;
//...
}
MMExampleCenterTableViewController.m:
-(void)doubleTap:(UITapGestureRecognizer*)gesture{
UIViewController *vc = [UIViewController new];
vc.view.backgroundColor = [UIColor redColor];
UIViewController *mainVC = [[UIApplication sharedApplication] keyWindow].rootViewController;
[mainVC presentViewController:vc animated:YES completion:^{
[vc dismissViewControllerAnimated:YES completion:nil];
}];
return;
[self.mm_drawerController bouncePreviewForDrawerSide:MMDrawerSideLeft completion:nil];
}
In the end, the problem was that I applied inset constraints on my mainViewController.
Behind the scene I suppose that method presentViewController:animated:completion: use frames and not AutoLayout, which break the UIView of the UIViewController that are in the mainViewController.
Thanks to #simalone, I used the same way to find the origin of the problem (use MMDrawerController example project).
I you want to test yourself the problem, I uploaded the example project with the issue so you can understand it.
There is a breakpoint on the line which is the origin of the bug.
Then just double click on the view.
Thanks again to #simalone. Cheers !

Issue Presenting View modally after a UISplitView loads

I'm new to UISplitView development, so I'm sure there is something obvious I'm doing wrong. I have a basic UISplitView iPad app that loads up with two UITableView controllers when the app launches. This works just fine.
What I am trying to do is immediately upon launch, presenting an "authentication" view modally so that a user will need to login before continuing. Here is the code I have so far which compiles and works without breaking, but the view is not showing.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
MasterViewController *masterViewController = [[MasterViewController alloc] initWithNibName:#"MasterViewController" bundle:nil];
UINavigationController *masterNavigationController = [[UINavigationController alloc] initWithRootViewController:masterViewController];
DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:#"DetailViewController" bundle:nil];
UINavigationController *detailNavigationController = [[UINavigationController alloc] initWithRootViewController:detailViewController];
self.splitViewController = [[UISplitViewController alloc] init];
self.splitViewController.delegate = detailViewController;
self.splitViewController.viewControllers = [NSArray arrayWithObjects:masterNavigationController, detailNavigationController, nil];
masterViewController.detailViewController = detailViewController;
masterViewController.managedObjectContext = self.managedObjectContext;
self.window.rootViewController = self.splitViewController;
[self presentAuthenticate];
[self.window makeKeyAndVisible];
applicationDidLaunch = YES;
return applicationDidLaunch;
}
- (void) presentAuthenticate {
AuthenticateViewController *loginController = [[AuthenticateViewController alloc] initWithNibName:#"AuthenticateViewController" bundle:nil];
[loginController setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
[loginController setModalPresentationStyle:UIModalPresentationFormSheet];
if ([self.splitViewController respondsToSelector:#selector(presentViewController:animated:completion:)]) {
[self.splitViewController presentViewController:loginController animated:NO completion:nil];
} else {
[self.splitViewController presentModalViewController:loginController animated:NO]; //iOS 4 works fine with or without animation
}
}
I defined the AuthenticateViewController as a View with a few textfields in it and have it wired to the File's Owners view.
Thanks ahead of time!
A viewcontroller will not allow to push/present on anotherview unless and until the view is complete loading.
Simple saying we are not allow to call presentModalViewController/pushViewController in a viewcontroller viewDidLoad/viewWillAppear. we need to call this in viewDidAppear.
I had the same issue you said.
Some Solution I can say are,
Do the loading of AuthenticateViewController after [self.window makeKeyAndVisible]; and in a performSelctor (may be with a delay).
Move the code to display AuthenticateViewController in SplitView's DetailView controller viewDidAppear.
thanks,
Naveen Shan

Resources