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

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];
}];
}

Related

How to clear Unsaved draft for contact in iOS?

Open the default contact form to add a number
Add a number and click on cancel to remove and open new modal for confirmation
Select discard changes modal close
Now when I open the contact book then first open the modal with the last number I added in the contact book form which added from my app
here is my code:
[self updateRecord:contact withData:contactData];
CNContactViewController *controller = [CNContactViewController viewControllerForNewContact:contact];
controller.delegate = self;
dispatch_async(dispatch_get_main_queue(), ^{
UINavigationController* navigation = [[UINavigationController alloc] initWithRootViewController:controller];
UIViewController *viewController = (UIViewController*)[[[[UIApplication sharedApplication] delegate] window] rootViewController];
while (viewController.presentedViewController)
{
viewController = viewController.presentedViewController;
}
[viewController presentViewController:navigation animated:YES completion:nil];
self->updateContactPromise = resolve;
Below is delegate method code:
[viewController dismissViewControllerAnimated:YES completion:nil];
if(updateContactPromise) {
if (contact) {
NSDictionary *contactDict = [self contactToDictionary:contact withThumbnails:true];
updateContactPromise(contactDict);
} else {
updateContactPromise(nil);
}
updateContactPromise = nil;
}
}

dismissViewControllerAnimated issue

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;

modalPresentationStyle from button click

I have a _exampleButton:
_exampleButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_exampleButton setBackgroundColor:[UIColor redColor]];
[_exampleButton addTarget:self action:#selector(certificatesButtonTouched) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:_exampleButton];
and the action:
-(void)certificatesButtonTouched
{
if(!_certificatesWindow)
{
_certificatesWindow = [[AWCertificatesViewController alloc] init];
UINavigationController *navController = [[UINavigationController alloc]
initWithRootViewController:_certificatesWindow];
navController.modalPresentationStyle = UIModalPresentationFormSheet;
[self presentViewController:navController animated:YES completion:nil];
[_certificatesWindow release];
}
else {
[_certificatesWindow.view removeFromSuperview];
[_certificatesWindow release];
_certificatesWindow = nil;
}
}
this presents the window in modal view controller from other class:
- (void)viewDidLoad
{
[super viewDidLoad];
UIBarButtonItem *cancelItem = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
target:self
action:#selector(cancel:)];
self.navigationItem.leftBarButtonItem = cancelItem;
UITableView *table = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame] style:UITableViewStylePlain];
[self.view addSubview:table];
_tableFromButton = table;
}
- (void)cancel:(id)sender
{
[self.presentingViewController dismissViewControllerAnimated:YES
completion:nil];
}
but after clicking the Cancel button, the Modal view controller view disappears, but if I click the _exampleButton again once- it will not appear, so I have to click it twice to show the modal view controller again. What is the problem?
It does this because that is what your code tell it to do...
You dismiss the view controller in -(void)cancel:(id)sender but this will not result in the _certificatesWindow iVar becoming nil in your first view controller. So when you touch the exampleButton again it will execute the else clause and clean up the _certificatesWindow view controller.
You should either use delegation or a code block to have the first view controller dismiss the second or remove the if/else test from your certificatesButtonTouched. Another alternative is to modify this method so that if _certificatesWindow is not nil it is reused -
-(void)certificatesButtonTouched
{
if(!_certificatesWindow)
{
_certificatesWindow = [[AWCertificatesViewController alloc] init];
}
UINavigationController *navController = [[UINavigationController alloc]
initWithRootViewController:_certificatesWindow];
navController.modalPresentationStyle = UIModalPresentationFormSheet;
[self presentViewController:navController animated:YES completion:nil];
}
But this may or may not be desirable depending on what the second view controller shows.
I would also suggest you look at converting to ARC if possible
The problem is after you tap "Cancel", _certificatesWindow still exists when you tap on certificatesButtonTouched. It will go to the Else statement on the First tap to make _certificatesWindow = nil.
if _certificatesWindow is not needed when you tap cancel, you might want to do the following:-
- (void)cancel:(id)sender
{
[self.presentingViewController dismissViewControllerAnimated:YES
completion:nil];
[_certificatesWindow.view removeFromSuperview];
[_certificatesWindow release];
_certificatesWindow = nil;
}
allocate view like this in view did load method
if(!_certificatesWindow)
{
_certificatesWindow = [[ViewController1 alloc] init];
}
-(void)certificatesButtonTouched
{
UINavigationController *navController = [[UINavigationController alloc]
initWithRootViewController:_certificatesWindow];
navController.modalPresentationStyle = UIModalPresentationFormSheet;
[self presentViewController:navController animated:YES completion:nil];
}
replace your code with this
-(void)certificatesButtonTouched
{
if(!_certificatesWindow)
{
_certificatesWindow = [[AWCertificatesViewController alloc] init];
}
UINavigationController *navController = [[UINavigationController alloc]
initWithRootViewController:_certificatesWindow];
navController.modalPresentationStyle = UIModalPresentationFormSheet;
[self presentViewController:navController animated:YES completion:nil];
[_certificatesWindow release];
}

EKEventview controller not showing navigation bar

I used the following code to view the event. But navigation bar doesn't visible.
EKEventViewController *addController = [[EKEventViewController alloc] initWithNibName:nil bundle:nil];
addController.event = self.event;
addController.allowsEditing = YES;
addController.allowsCalendarPreview = YES;
[self.navigationController presentViewController:addController animated:YES completion:nil];
with Present viewcontroller you need to Add sepreated NavigationController for UIViewcontroller like:-
EKEventViewController *addController = [[EKEventViewController alloc] initWithNibName:#"EKEventViewController" bundle:nil];
UINavigationController *navController=[[UINavigationController alloc]initWithRootViewController:addController];
addController.event = self.event;
addController.allowsEditing = YES;
addController.allowsCalendarPreview = YES;
if ([self respondsToSelector:#selector(presentViewController:animated:completion:)])
{
[self presentViewController:navController animated:YES completion:nil];
}
else
{
[self presentModalViewController:navController animated:YES];
}

Problems presenting a second modal view controller

At the moment my error says my view controller is already presenting null. I think i have the right idea with my below code, but I'm not implementing it correctly. Thanks for your help
-(IBAction)datePicker
{
//UIViewController *presenter = self.presentingViewController;
[self dismissViewControllerAnimated:YES completion:^{
/*
UIStoryboard* sb = [UIStoryboard storyboardWithName:#"MainStoryboard.storyboard"
bundle:nil];
UIViewController* dateView = [sb instantiateViewControllerWithIdentifier:#"DatePickerViewController"];
*/
[self performSegueWithIdentifier:#"dueDateSegue" sender:self];
//[presenter presentViewController:dateView animated:YES completion:nil];
/*
[self presentViewController:dateView animated:YES completion:^{
UIBarButtonItem *saveDate = [[UIBarButtonItem alloc]
initWithTitle:#"Save Date"
style:UIBarButtonItemStyleDone
target:self
action:#selector(labelDatePicker)];
self.navigationItem.rightBarButtonItem = saveDate;
pick = [[UIDatePicker alloc] init];
[pick setFrame:CGRectMake(0,200,320,120)];
//[pick addTarget:self action:#selector(done) forControlEvents:UIControlEventValueChanged];
//dateFieldText.delegate = self;
//dateFieldText.inputView = pick;
}];
*/
}];
}
In the method datePicker add the following line as the first line of code:
UIViewController *presenter = self.presentingViewController;
And then instead of :
[self presentViewController:dateView animated:YES completion:nil];
try using:
[presenter presentViewController:dateView animated:YES completion:nil];

Resources