presentViewController导致错误"视图不在窗口层次结构中"

时间:2015-05-04 06:45:15

标签: ios presentviewcontroller

在FirstViewController中,我在 viewdidload 中调用了一个函数,它返回一个值。

如果返回值为1,我想呈现NextViewController如果返回值为0我想在FirstViewController上进行一些操作。

这是我的代码:

- (void)viewDidLoad
{
    ViewController *view1=[[ViewController alloc]init];
    int ret=[view1 getcustId];
    NSLog(@"the ret is %d",ret);
    if (ret==1)
    {
        Nextviewcontroller *nextview=[[Nextviewcontroller alloc]initWithNibName:nil bundle:nil];
        [self presentViewController:nextview animated:YES completion:NULL];
        [super viewDidLoad];

    }
    else
    {
        im=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"591637.png"]];
        im.frame=CGRectMake(0,0,390,670);
        im.userInteractionEnabled=YES;
        [self.view addSubview:im];
        [super viewDidLoad];

    }
}

但是我在呈现NextViewController时遇到错误。

Warning: Attempt to present <Nextviewcontroller: 0x7f93b0e41780> on <ViewController: 0x7f93b0f3b630> whose view is not in the window hierarchy!

如何从viewDidLoad呈现NExtViewController?请帮忙

3 个答案:

答案 0 :(得分:1)

您可以先尝试拨打[super viewDidLoad]

吗?
- (void)viewDidLoad {
  [super viewDidLoad];
  ViewController *view1=[[ViewController alloc] init]; //this line
  int ret = [view1 getcustId]; //and this line doen't make sense
  NSLog(@"the ret is %d",ret);
  if (ret == 1) {
    Nextviewcontroller *nextview=[[Nextviewcontroller alloc]initWithNibName:nil bundle:nil];
    [self presentViewController:nextview animated:YES completion:NULL];
  }
  else {
    im = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"591637.png"]];
    im.frame = CGRectMake(0, 0, 390, 670);
    im.userInteractionEnabled = YES;
    [self.view addSubview:im];
  }
}

同时显示已提供ViewController

的代码

答案 1 :(得分:0)

这个问题是因为viewController呈现动画。你试图在完成前一个viewController的外观之前呈现第二个视图。

试试这个:

- (void)viewDidLoad
{
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        ViewController *view1=[[ViewController alloc]init];
        int ret=[view1 getcustId];
        NSLog(@"the ret is %d",ret);
        if (ret==1)
        {
            Nextviewcontroller *nextview=[[Nextviewcontroller alloc]initWithNibName:nil bundle:nil];
            [self presentViewController:nextview animated:YES completion:NULL];
            [super viewDidLoad];

        }
        else
        {
            im=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"591637.png"]];
            im.frame=CGRectMake(0,0,390,670);
            im.userInteractionEnabled=YES;
            [self.view addSubview:im];
            [super viewDidLoad];

        }

    });
}

答案 2 :(得分:-1)

  1. viewDidAppear

  2. 中编写代码
  3. 将nib名称放在initWithNibName中而不是nil

     -(void)viewDidAppear:(BOOL)animated
    
     {
    [super viewDidAppear:YES];
    
    ViewController *view1=[[ViewController alloc]init];
    int ret=[view1 getcustId];
    NSLog(@"the ret is %d",ret);
    if (ret==1)
    {
        Nextviewcontroller *nextview=[[Nextviewcontroller alloc]initWithNibName:@"Nextviewcontroller" bundle:nil];
        [self presentViewController:nextview animated:YES completion:NULL];
    }
    else
    {
        im=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"591637.png"]];
        im.frame=CGRectMake(0,0,390,670);
        im.userInteractionEnabled=YES;
        [self.view addSubview:im];
     }
    }