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

有帮助吗?

解决方案

你可以添加一个函数 - (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。 希望我能理解你的问题......

其他提示

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

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

然后父母会这样做:

someView.fooDelegate = self;

然后视图将执行以下操作:

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

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

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

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

这是另一种方式:

SuperviewsViewController *controller = self.superview.nextResponder;

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

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

Apple的UIResponder参考

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top