![](/img/trans.png)
[英]How to implement close a subview when user touched its superview in 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.