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

发布于 2024-11-26 22:33:51 字数 572 浏览 2 评论 0原文

我现在正在尝试在两种视图之间切换。问题是如何调用它。

这是解释我的情况的最简单方法:

我有一个家长视图。 有一个子类 ChildView,其中包含一个表。 在该表中选择一个对象后,我希望切换到该父视图的不同子视图。

家长--------- |Child 1 |Child 2

Child 1 是 Parent 的子类,允许我访问 Parent 中在 Child Views 1 和 2 之间切换的方法,但由于某种原因,从 Child 1 访问它时它不起作用。

有关如何的任何线索做这个吗?基本代码如下:

Child 1 - (void)changeViews

[super methodToSwitchChildViews];

父级 - (void)方法切换视图

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

空城缀染半城烟沙 2024-12-03 22:33:51

Super 是继承中位于(子)类之前的类。在这里,孩子们似乎是超级视图(父母)的视图。所以使用superview,而不是super。

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.

他不在意 2024-12-03 22:33:51

好吧,我查了很多资料,终于找到了解决方案。如果有人遇到同样的问题,您可以这样做:

在子视图的 .h 文件中执行

@class parentViewName

然后在 .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];
}

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];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文