0

So I have an app where I'm implementing a universal tool bar that sort of acts like a tab bar + tool bar and is featured on all of my view controllers. I have a custom tool bar that has some buttons that manipulate some navigational view controllers I've created in the App Delegate. However, I am having trouble getting these buttons to work, below is an example of an action method for one of the buttons I'm using:

    Test_ClassAppDelegate *test_ClassAppDelegate = (Test_ClassAppDelegate*)[[UIApplication sharedApplication] delegate];
[[[test_ClassAppDelegate window] superview] removeFromSuperview];
[[test_ClassAppDelegate window] addSubview:[[test_ClassAppDelegate helpNavController]view]];
[test_ClassAppDelegate release];    

I think the main area I might be going wrong is in my second line where I'm removing my superview. I'm not even sure if this is the correct way to go about doing that. I know I could implement this code in the app delegate itself, but am curious is there anyway to remove the superview from else where, or if anyone else sees any problems with this code. Thanks.

Ser Pounce
  • 14,196
  • 18
  • 84
  • 169

1 Answers1

0

Yes, that line is certainly a problem...

From the UIWindow reference:

The window is the root view in the view hierarchy.

Thus, a window has no superview, and you're asking the window's superview to remove itself from its superview. This doesn't work for the same reason that it's such a tongue-twister--it just doesn't make sense.

Furthermore, -removeFromSuperview doesn't remove a superview, it removes the view to which you are sending -removeFromSuperview from its superview. Do you see the difference?

It sounds like you're wanting to change the root view controller based on a user selection. If that's the case, see my answer to this question, and lots of others on SO.

Community
  • 1
  • 1
GarlicFries
  • 8,095
  • 5
  • 36
  • 53
  • Thanks that worked, however when I tried to switch to another rootviewcontroller, it froze up. There is a memory leak somewhere. Does the rootviewcontroller that gets replaced have to be reinitialized or deallocated? – Ser Pounce Aug 10 '11 at 23:07
  • Without seeing your code, it's hard to say. But yes, the memory-management rules are the same here as ever. – GarlicFries Aug 14 '11 at 21:54