繁体   English   中英

在iPhone中向其添加子视图时如何保留超级视图?

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

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

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。这是怎么发生的呢?毕竟它是一个appdelegate变量。当我删除他添加的视图然后去背景在那里。有人可以指出我要去哪里了吗?

检查您的代码,发现viewController不是nil。通过在此方法中应用一些代码来检查自己

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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