【问题标题】:Removing a superview from a method call inside that class从该类中的方法调用中删除超级视图
【发布时间】:2011-07-29 05:59:45
【问题描述】:

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

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

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

父母--------- |孩子 1 |孩子 2

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

关于如何做到这一点的任何线索?基本代码如下:

孩子 1 - (void) changeViews

[super methodToSwitchChildViews];

家长 - (void) methodToSwitchViews

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

【问题讨论】:

  • 你的孩子是你父母的子类是什么意思?这仅意味着孩子具有其父母的属性,而不是与他的父母有联系。

标签: iphone objective-c uiview class-design superclass


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

    在子视图的.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];
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多