Search code examples
iphoneobjective-cuiviewclass-designsuperclass

Removing a superview from a method call inside that class


I'm trying to switch between two views right now. The problem is how it is called.

Heres the easiest way to explain my situation:

I have a Parent View. With a subclass ChildView, that contains a table. Upon selecting an object in that table, I wish to switch to a different child view of that parent view.

Parent--------- |Child 1 |Child 2

Child 1 is a subclass of Parent to allow me to access a method in Parent that switches between Child Views 1 and 2, but for some reason it wont work when accessing it from Child 1.

Any clues on how to do this? Heres the basic code:

Child 1 - (void) changeViews

[super methodToSwitchChildViews];

Parent - (void) methodToSwitchViews

[self.child1.view removeFromSuperView];
[self.view insertSubView:child2.view atindex:0];

Solution

  • Okay, I dug around quite a bit and finally figured out a solution. In case anybody ever has the same problem here's what you do:

    In the .h file of the child view do

    @class parentViewName
    

    Then in the .m file add

    #import "parentViewName.h"
    
    ...
    
    - (void) functionToRemoveSelfFromView {
       parentViewName *PARENT = [[parentViewName alloc] init];
    
       // You must have a method in the parent view to toggle or remove the subview, the way
       // you want it done, then call it with the new delegate. Make sure it doesn't set this 
       // view to nil or releases it because this method has yet to return. If animating do not
       // hide this view either.
    
       [PARENT methodToRemoveSelfFromView];
       [PARENT release];
    }