【问题标题】:UIView parent referenceUIView 父参考
【发布时间】:2012-01-26 20:19:18
【问题描述】:

如果我知道 UIView 已添加到 父项,是否有一种本地方式可以从 子项 中访问 父项 >?

例如。 [self parent] // returns parent UIView

【问题讨论】:

    标签: ios cocoa-touch uiview


    【解决方案1】:

    当然:UIView 具有 superview 属性。

    【讨论】:

      【解决方案2】:

      你可以试试UIViews property superview

      [self superview]
      

      【讨论】:

        【解决方案3】:

        在 Childs .h 中为父级创建一个属性:

        ParentView *parentView;
        
        @property (nonatomic, retain) ParentView *parentView;
        

        (确保@synthesize 您在 Childs .m 中的属性)

        然后,当您分配子实例时,立即将 self 分配给 parentView 属性

        在父 .m 中:

        Child *child = [[Child alloc] init];
        [child setParentView:self];
        

        然后您可以通过以下方式从子级访问父视图:

        [self parentView];
        

        【讨论】:

        • 这不是 OP 要求的 native 方式。 UIView 中的 superview 属性用作实现此目的的本机方式。另一个注意事项:现在没有必要再合成属性了。 :-}
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-04-18
        • 2015-10-26
        • 2012-03-20
        • 1970-01-01
        • 2012-06-11
        相关资源
        最近更新 更多