简体   繁体   English

从该类内部的方法调用中删除超级视图

[英]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. 对于子类ChildView,该子类包含一个表。 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 父母--------- |孩子1 |孩子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. 子级1是父级的子类,允许我访问父级中在子级视图1和2之间切换的方法,但是由于某些原因,当从子级1访问它时,它将无法工作。

Any clues on how to do this? 有关如何执行此操作的任何线索? Heres the basic code: 这是基本代码:

Child 1 - (void) changeViews 子1- (void)changeViews

[super methodToSwitchChildViews];

Parent - (void) methodToSwitchViews 父级 -(void)methodToSwitchViews

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

Super is the class that precedes a (sub)class in inheritance. Super是继承中(子)类之前的类。 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 在子视图的.h文件中

@class parentViewName

Then in the .m file add 然后在.m文件中添加

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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