0

私は以下のビュー階層を持っています:その中のナビゲーションコントローラ、私はセル内にカスタムUIButtonを持つUITableViewを含む別のビューコントローラを押しました。私は別のビューコントローラ(MyCustomViewController2)を持っていますが、これはアニメーションを使って上に示したいものです。しかし、私はこの階層と混同しています。私はカスタムUIButtonクラスの- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)eventメソッドを上書きする方法を知らない。これまでに来たコードは次のとおりです:目的C:カスタムUIButtonクラスからView Controllerを表示する方法は?

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    MyCustomViewController2 *myVC = [[UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:@"MyVC"]; 
    [self.window addSubview: myVC.view]; 
} 

しかし、それはとても悪いです!私はアニメーションを持っていないし、削除するために削除する必要があります...誰かが私に救いの手を差し伸べることができますか?

答えて

0

It's highly recommended not to subclass a UIButton.だから私はしません。

しかしUIButtonはUicontrolののサブクラスであることから、あなたは、このメソッドを直接使用することができます。

- (void)addTarget:(id)target 
      action:(SEL)action 
forControlEvents:(UIControlEvents)controlEvents 

例のコードでは、次のようになります。

[self.myButton addTarget:self action:@selector(showOtherVC) forControlEvents: UIControlEventTouchUpInside]; 

これは、ターゲットを見ていきます(自己)特定の方法の場合(showOtherVC)ユーザーがボタンから指を離したとき。

あなたは、現在のビューコントローラは、モーダル例として、本のUIViewControllerメソッドを使用して、新しいものを提示するかもしれない:

-(void)showOtherVC 
{ 
    MyCustomViewController2 *myVC = [[UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:@"MyVC"]; 
    [self presentViewController:myVC animated:true completion:nil]; 
} 

よりこちらのUicontrolに読む: UIControl Documentation

そして、他のUIViewControllerモーダル遷移をチェックしてくださいここのスタイル: UIViewController Documentation

関連する問題