6

I've set up a navController, which appears after tapping a button. However, if I tap the button I get the error of: "Warning: Attempt to present <UINavigationController>: 0xab5d9d0 on <MyApp: 0xadaa320> whose view is not in the window hierarchy!"

Does anyone know how to solve this? I also tried something on Stackoverflow but it wasn't my solution.

Here my code for opening the navigationcontroller:

I dont know if somebody know this photogallery but if you don't, here is the project.

My code (MyApp.m):

#import MyApp.h
...
//some stuff
- (void)launchGalleryView
{



    MWPhotoBrowser *browser = [[MWPhotoBrowser alloc] initWithDelegate:self];

    // Set browser options.
    browser.wantsFullScreenLayout = YES;
    browser.displayActionButton = NO;


    UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:browser];

    NSMutableArray *photos = [[NSMutableArray alloc] init];
    MWPhoto *photo;
    photo = [MWPhoto photoWithFilePath:[[NSBundle mainBundle] pathForResource:@"callculator" ofType:@"jpg"]];
    photo.caption = @"The calculator is soo beateful...";
    [photos addObject:photo];

    self.photos = photos;

    [self presentModalViewController:navController animated:NO];
}

Thanks in advance.

Edit:

it is in the recources and in the compile sources but in the resources you can see that it is red (the storyboard). Maybe it's caused by this?

The Second controller .h:

@class MyApp;

@interface Second : UIViewController <MWPhotoBrowserDelegate> {

}



@property (nonatomic, retain) MyApp* vC;

@end

The Secnond controller .m:

#import "Second.h"
#import "MyApp.h"


@interface Second ()

@end

@implementation Second

@synthesize vC;
    //some stuff in here


//the action 
    - (IBAction)dothis:(id)sender {

        NSLog(@"launch the navcontroller");


        [self.vC launchGalleryView];

    }

MyApp.h:

#import "Second.h"


@interface myApp : UIViewController  <MWPhotoBrowserDelegate> {
    }

-(void)launchGalleryView;

NSArray *_photos;

enter image description here

NEW EDIT:

I found that I have to call the method "launchGalleryView" in the viewDidAppear but how can I do this without calling the navcontroller everytime the view loads? Does anyone know how to do this?

MasterRazer
  • 1,377
  • 3
  • 16
  • 40
  • I get that error when I try to present a controller from a controller that is not the topmost at the moment. Make sure is the top most and it's not presenting anything else! – yuf Dec 15 '12 at 00:42
  • What if you call [self dismissViewControllerAnimated:NO completion:nil]; before the presentModalViewController? What happens? – yuf Dec 15 '12 at 00:57
  • Where in your app is this method called? You get this error because the calling controller's view isn't in the hierarchy yet -- like if you try to present another controller from the init or viewDidLoad method. Try calling it later, like in viewDidAppear. – rdelmar Dec 15 '12 at 05:20
  • in which controller is ur `launchGallery` method.. it should be in the window hierarchy.. – vishy Dec 15 '12 at 05:40
  • it is in the mainview Controller – MasterRazer Dec 15 '12 at 11:34
  • vishy it is the same source I gave you but the controllers name is another it's called MyApp.h/.m and it has a connected interface in the storyboard – MasterRazer Dec 15 '12 at 11:35
  • What class does `MyApp` inherit from? If it's some kind of view controller, how is `MyApp` given control of the display? What's the code in the `IBAction` that fires when you tap the button and what class is that action in? – Phillip Mills Dec 15 '12 at 14:18
  • MyApp is a UIViewcontroller class and the action is an IBAction...look at my Edit! – MasterRazer Dec 15 '12 at 14:50
  • It appears to be complaining because `self.vC` and/or `self.vC.view` are not part of the hierarchy that's currently controlling the display. – Phillip Mills Dec 15 '12 at 15:00
  • vC is set as the MyApp controller. In the other controller where the action is I've implemented the file and made the @class and property. – MasterRazer Dec 15 '12 at 15:14
  • It actually seems to work fine and If I put in a NSLog in the launchGallery mehtod I get it in the output. It's just the navController who cause the error – MasterRazer Dec 15 '12 at 15:15
  • I still don't understand your view controller structure. You say `MyApp` is a `UIViewcontroller` but it's a property of some other view controller that handles the button tap.... I think you need to have whichever view controller is currently in charge of the screen (`self`??) be the one that calls `presentModalViewController:`. – Phillip Mills Dec 15 '12 at 15:45
  • I've put in some code take a look – MasterRazer Dec 15 '12 at 15:54
  • That code makes it look as if `MyApp` has no reason to exist. `Second` is the same kind of delegate, knows about `photos`, and is probably the current view controller. Let it present the navigation controller and see if that fixes this error. – Phillip Mills Dec 15 '12 at 16:04
  • http://stackoverflow.com/questions/13777395/display-navigationcontroller-on-top That was my problem – MasterRazer Dec 15 '12 at 16:06
  • @vishy I sent your code to a friend of mine and he told me it doesn't worked for him. But why did it worked for me and I get this error just in my project? – MasterRazer Dec 16 '12 at 01:12
  • This changes as per the requirement & need.. for your case, just you need to remove pop-up and just present the photo gallery on any Nearest View Controller.. – vishy Dec 16 '12 at 15:28
  • I just found someone who awnsered my question but thatnk you – MasterRazer Dec 16 '12 at 15:41
  • And the problem was that there was no viewcotroller found – MasterRazer Dec 16 '12 at 15:41
  • http://stackoverflow.com/a/24061440/884674 – jeet.chanchawat Dec 31 '14 at 10:47

1 Answers1

24

i checked your project.. wasn't able to sort out the proper issue..

but i tried a hack and it worked..

replace this line with

[self presentModalViewController:navController animated:YES];

this

[[[[[UIApplication sharedApplication] delegate] window] rootViewController] presentModalViewController:navController animated:YES];
Shubhank
  • 21,721
  • 8
  • 65
  • 83