【问题标题】:Whose view is not in window hierarchy issue谁的视图不在窗口层次结构问题中
【发布时间】:2012-12-03 00:12:04
【问题描述】:

我设置了一个 navController,它在点击按钮后出现。但是,如果我点击按钮,我会收到以下错误:“Warning: Attempt to present <UINavigationController>: 0xab5d9d0 on <MyApp: 0xadaa320> whose view is not in the window hierarchy!

有谁知道如何解决这个问题?我也在 Stackoverflow 上尝试了一些东西,但这不是我的解决方案。

这是我打开导航控制器的代码:

我不知道是否有人知道这个相册,但如果你不知道,here 就是这个项目。

我的代码 (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];
}

提前致谢。

编辑:

它在资源和编译源中,但在资源中你可以看到它是红色的(故事板)。可能是这个原因?

第二个控制器.h:

@class MyApp;

@interface Second : UIViewController <MWPhotoBrowserDelegate> {

}



@property (nonatomic, retain) MyApp* vC;

@end

Secnond 控制器 .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;

新编辑:

我发现我必须在 viewDidAppear 中调用方法“launchGalleryView”,但是如何在每次加载视图时不调用 navcontroller 来执行此操作?有人知道怎么做吗?

【问题讨论】:

  • 当我尝试从当前不是最顶层的控制器中呈现控制器时出现该错误。确保 是最上面的并且它没有呈现任何其他内容!
  • 如果调用 [self dismissViewControllerAnimated:NO completion:nil];在presentModalViewController 之前?会发生什么?
  • 这个方法在你的应用程序的什么地方被调用?您会收到此错误,因为调用控制器的视图尚未在层次结构中 - 就像您尝试从 init 或 viewDidLoad 方法呈现另一个控制器一样。稍后尝试调用它,例如在 viewDidAppear 中。
  • 哪个控制器是你的launchGallery方法..它应该在窗口层次结构中..
  • 它在主视图控制器中

标签: iphone ios uinavigationcontroller


【解决方案1】:

我检查了您的项目.. 无法解决正确的问题..

但我尝试了一个 hack,它成功了..

将此行替换为

[self presentModalViewController:navController animated:YES];

这个

[[[[[UIApplication sharedApplication] delegate] window] rootViewController] presentModalViewController:navController animated:YES];

【讨论】:

猜你喜欢
  • 2014-07-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-03
相关资源
最近更新 更多