iphone / ipad添加添加和删除子视图不起作用

时间:2010-05-18 03:54:31

标签: iphone cocoa-touch ipad

我在为我的场景添加新视图时遇到了一些麻烦,我有这样的代码:

- (void) showMyDayView {
NSLog(@"My Day View was touched");

MyDayViewController *temp = [[MyDayViewController alloc] initWithNibName: @"MyDayView" bundle:nil];
self.myDayViewController = temp;

NSLog(@"superview: %@", [[self mainNavView] superview]);
[[self mainNavView] removeFromSuperview];
NSLog(@"after removal main: %@", [self mainNavView]);
NSLog(@"after removal view: %@", [self view]);
NSLog(@"after removal superview: %@", [[self view] superview]);

[[[self view] superview] addSubview: [self.myDayViewController view]];

[temp release];
}

当我运行此代码时,控制台会说“删除superview后:( null)”

所以当我将subView添加到superview时,没有任何反应,因为superview为null。

有什么想法吗?

由于 标记

1 个答案:

答案 0 :(得分:1)

如果要重复使用要删除的视图,请首先保留它。 removeFromSuperview释放它被调用的任何视图。

因此...

[[self mainNavView] retain]
[[self mainNavView] removeFromSuperview];

[self mainNavView]仍然可以安全使用。