繁体   English   中英

iphone sdk - 调用视图的superview的viewcontroller方法

[英]iphone sdk - calling a method of view's superview's viewcontroller

你好我怎么能在当前视图中调用一个在当前视图的superview的viewcontroller中实现的方法? 你能帮我吗。 感谢名单

通常,这是通过代表完成的。

让您的视图接口定义协议和对某个委托的引用。 然后让您的父视图控制器实现此协议。

然后父母会这样做:

someView.fooDelegate = self;

那么视图会做这样的事情:

if(self.fooDelegate != nil) {
   if([fooDelegate respondsToSelector:...]) {
      [fooDelegate performSelector:...];
   }
}

这不是编译,但我认为你得到了要点。

UIViews不了解他们的视图控制器。 您将需要创建一个自定义UIView子类,该子类维护对一个(或可能多个)视图控制器的引用,尽管这样做会引入UIView和UIViewController之间的进一步耦合。

您应该考虑在superview或view的类中实现该方法,而不是在视图控制器中实现它。

你可以添加一个函数 - (void)initWithView:(EchiquierDisplayView *)aSuperview或类似的东西,在你的中定义一个引用

@interface pieceDraggableImageView : UIImageView { 
CGPoint startLocation; 
CGPoint startLocationInView;
EchiquierDisplayView *theSuperview;  
} 

@property (nonatomic, retain) EchiquierDisplayView *theSuperview;

-(void)correctDestinationPosition; 
-(void)initWithView:(EchiquierDisplayView *)aSuperview; 
...
-(void)askSuperview;
@end

@implementation pieceDraggableImageView

...
-(void)initWithView:(EchiquierDisplayView *)aSuperview
{
   theSuperview = aSuperview;
}
...
-(void) correctDestinationPosition
{
   [theSuperview correctIt];
}

现在一定要在superview中实现函数correctIt。 希望我理解你的问题是正确的......

这是另一种方式:

SuperviewsViewController *controller = self.superview.nextResponder;

if (controller && [controller isKindOfClass:[SuperviewsViewController class]])
{
    [controller method];
}

它应该适用于大多数情况。

Apple的UIResponder参考

暂无
暂无

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

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