views:

225

answers:

1

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

+1  A: 

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.

Adam Eberbach
so is the reason why the superview is made null because the mainNavView is released?
Mark
but still after adding the retain, its still nulled out
Mark
There must be something else releasing it then, adding a retain takes care of the removeFromSuperview. I'm still a bit confused about what you're doing, the identity and function of all your views isn't given.
Adam Eberbach
im trying to insert a new view into my scene, my new view will be a uisplitview, hopefully, but im also not having much luck.
Mark
Have you mapped out exactly what relation these views have to each other? I'm not seeing what relation or relevance myDayViewController is apart from being some valid view to add as subview. Is [self view] the same object as [self mainNavView]? If so is it being changed by something else?
Adam Eberbach
yeah sorry about that, im very new to all this still, I had a lot of spaghetti code lying around, Ive now cleaned it up, and your code is working fine :)
Mark
good luck, stick with it.
Adam Eberbach