简体   繁体   中英

How to retain superview when adding a subview to it in iphone?

I had an application in which i am having a view controller which is an appdelegate instance.I am adding that view to the self.navigationcontrollers view in order to place it above navigation bar.lke this `

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

` and then from that view controller when i am going in to the background of the application ...that appdelegate viewcontroller is showing as nil.How come this happen.after all its an appdelegate variouble.when i remove he added view and then go background it is there.Can anybody point me in where i am going wrong?

Checked you code.And found viewController is not nil.Check yourself by applying some code in this method

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM