开发者

iphone/ipad adding adding and removing a subview doesnt work

Im have a slight amount of trouble adding a new view to my scene, I have the code like this:

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

And when I run this code, the console says "after removal superview: (null)"

so when I add the subView to the superview, nothing 开发者_如何学编程happens because the superview is null.

Any ideas?

Thanks Mark


If you want to reuse a view that you are going to removeFromSuperview, you must retain it first. removeFromSuperview releases any view it is invoked on.

So...

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

And [self mainNavView] remains safe to use.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜