How to access superview from another class?
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.
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.
精彩评论