在iphone中添加子视图时如何保留superview?

时间:2013-07-10 06:32:29

标签: iphone ios ipad

我有一个应用程序,其中我有一个视图控制器,这是一个appdelegate实例。我将该视图添加到self.navigationcontrollers视图,以便将它放在导航bar.lke这个`

if(appDelegate.viewcontroller==nil)
    {

        appDelegate.viewcontroller = [[ViewController alloc] init];
        [self.navigationController.view addSubview:appDelegate.viewcontroller.view];
        NSLog(@"My view frame: %@", NSStringFromCGRect(appDelegate.viewcontroller.view.frame));

        appDelegate.viewcontroller.view.tag=7;


        appDelegate.viewcontroller.view.frame =CGRectMake(0,480,300,460);
        [UIView animateWithDuration:.50 
                         animations:^{
       appDelegate.viewcontroller.view.frame =CGRectMake(0,30,300,440);
                         }];
        [appDelegate.viewcontroller viewWillAppear:YES];

    }
    else
    { 
        NSLog(@"My view frame: %@", NSStringFromCGRect(appDelegate.viewcontroller.view.frame));
        appDelegate.viewcontroller.view.frame=CGRectMake(0, 30,300, 440); 



        [self.navigationController.view addSubview:appDelegate.viewcontroller.view];


        appDelegate.viewcontroller.view.frame =CGRectMake(0,480,300,460);
        [UIView animateWithDuration:.50 
                         animations:^{
                             appDelegate.viewcontroller.view.frame =CGRectMake(0,30,300,440);
                         }];
       [appDelegate.viewcontroller viewWillAppear:YES];

    }

...Now i am adding another viewcontrollers view in to this view by

contactphoneForAddfriend= [[EventsViewController alloc]initWithNibName:@"EventsViewController" bundle:nil];
    contactphoneForAddfriend.orgarray=self.eventsArray;


    [UIView beginAnimations:@"View Flip" context:nil];
    [UIView setAnimationDuration:0.80];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];

    [UIView setAnimationTransition:
     UIViewAnimationTransitionFlipFromRight
                           forView:self.view cache:NO];


     [self.view addSubview:contactphoneForAddfriend.view];
    [UIView commitAnimations];  

`然后从那个视图控制器进入应用程序的后台... appdelegate viewcontroller显示为nil.How会发生这种情况。所有这是appdelegate variouble.when我删除他添加了视图然后去背景它就在那里。任何人都可以指出我哪里出错了?

1 个答案:

答案 0 :(得分:0)

检查了你的代码。发现viewController不是nil。通过在这个方法中应用一些代码来检查自己

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    if (self.viewController==nil) {
        NSLog(@"ViewController is nil");
    }else{
        NSLog(@"ViewController Has value %@",NSStringFromClass([self.viewController class]));
    }
}