简体   繁体   English

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

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

hello how can i call in the current view, a method implemented in the viewcontroller of the current view's superview? 你好我怎么能在当前视图中调用一个在当前视图的superview的viewcontroller中实现的方法? can you help me please. 你能帮我吗。 thanx 感谢名单

Typically this is done through delegates. 通常,这是通过代表完成的。

Have your view interface define a protocol and a reference to some delegate. 让您的视图接口定义协议和对某个委托的引用。 Then have your parent viewcontroller implement this protocol. 然后让您的父视图控制器实现此协议。

Then the parent would do this: 然后父母会这样做:

someView.fooDelegate = self;

then the view would do something like this: 那么视图会做这样的事情:

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

This is not compiled, but I think you get the gist. 这不是编译,但我认为你得到了要点。

UIViews have no knowledge of their view controllers. UIViews不了解他们的视图控制器。 You will need to create a custom UIView subclass that maintains a reference to one (or potentially more than one) view controller, although doing so introduces further coupling between UIView and UIViewController. 您将需要创建一个自定义UIView子类,该子类维护对一个(或可能多个)视图控制器的引用,尽管这样做会引入UIView和UIViewController之间的进一步耦合。

You should consider implementing the method in the superview's or view's class rather than implementing it in a view controller. 您应该考虑在superview或view的类中实现该方法,而不是在视图控制器中实现它。

You can add a function -(void)initWithView:(EchiquierDisplayView *)aSuperview or something like that, define a reference in your 你可以添加一个函数 - (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];
}

Now be sure to implement the function correctIt in your superview. 现在一定要在superview中实现函数correctIt。 Hopefully i understood your question right... 希望我理解你的问题是正确的......

Here is another way: 这是另一种方式:

SuperviewsViewController *controller = self.superview.nextResponder;

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

It should work in most cases. 它应该适用于大多数情况。

Apple's UIResponder Reference Apple的UIResponder参考

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

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