简体   繁体   中英

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

Super is the class that precedes a (sub)class in inheritance. Here the children seem to be views on a superview (parent). So use superview, and not super.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM