iPhone SDK-ビューのスーパービューのViewControllerのメソッドを呼び出す

StackOverflow https://stackoverflow.com/questions/1405942

  •  05-07-2019
  •  | 
  •  

質問

こんにちは、現在のビューのスーパービューのビューコントローラーに実装されているメソッドである現在のビューでどのように呼び出すことができますか? 助けてください。 thanx

役に立ちましたか?

解決

関数-(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];
}

これで、スーパービューに関数correctItを必ず実装してください。 うまくいけば、あなたの質問を正しく理解しました...

他のヒント

通常、これはデリゲートを通じて行われます。

ビューインターフェイスでプロトコルとデリゲートへの参照を定義します。次に、親ViewControllerにこのプロトコルを実装させます。

その後、親はこれを行います:

someView.fooDelegate = self;

ビューは次のようになります:

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

これはコンパイルされていませんが、要点はわかります。

UIViewsは、View Controllerの知識がありません。 1つ(または複数のビュー)コントローラーへの参照を保持するカスタムUIViewサブクラスを作成する必要がありますが、そうすると、UIViewとUIViewControllerの間にさらなる結合が導入されます。

View Controllerでメソッドを実装するのではなく、スーパービューまたはビューのクラスでメソッドを実装することを検討する必要があります。

別の方法を次に示します。

SuperviewsViewController *controller = self.superview.nextResponder;

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

ほとんどの場合、動作するはずです。

AppleのUIResponderリファレンス

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top