2011-03-03 4 views
12

UIImageViewのUIButtonsを一括して使用しています。だから、私はこれらの2つを永久に結婚するカスタムクラスを作成して、少し簡単にしました。それは私が - (id)initWithObject:(AUIImageViewButton *)imageViewButtonを実装することを決定するまでうまく動作します。UIButtonターゲットイベント、アクションイベント、コントロールイベントを取得するには?

明らかに、渡されるimageViewButtonオブジェクトから関連するすべてのプロパティをコピーする必要があります。 UIImageViewはまったく問題ありません。このような何かがそれを扱う:

button = [UIButton buttonWithType:imageViewButton.button.buttonType];     // Copy all relevant data from the source's button 
button.frame = imageViewButton.imageview.frame;           // 
[button setTitle:imageViewButton.button.titleLabel.text forState:UIControlStateNormal]; // 
button.tag = imageViewButton.button.tag;            // 

私はaddTargetのすべてのデータを取得する方法を考え出す少し問題を抱えています:アクションボタンのもののほとんど

imageview = [[UIImageView alloc] initWithFrame:imageViewButton.imageview.frame];  // Copy all relevant data from the source's imageview 
[imagebutton.imageview setBackgroundColor:imageViewButton.imageview.backgroundColor]; // 
[imagebutton.imageview setImage:imageViewButton.imageview.image];      // 

も容易に利用可能です:forControlEventsメソッド。

ドキュメントを見ると、UIControlのallControlEventsメソッドとallTargetsメソッドを使用できることがわかります。私は今それを掘り下げ、私がどれくらいの苦労を受けるのかを見ていきます。私が確信しているのはその行動です。

誰も私に正しい方向に押しつけることができますか?

おかげで、

-Martin

+0

をちょうどチェックし、あなたはUIButtonが背景画像(タイトルテキストが上に表示される)と画像(タイトルテキストが表示されない)の両方をサポートすることを知っていますか? UIImageViewのどの機能が必要ですか? – Bogatyr

答えて

31

するuicontrolのallTargetsallControlEventsを起動する方法です。パズルの最後の部分はactionsForTarget:forControlEvent:です。ターゲットとイベントごとに1回呼びます。

+0

ああ!それはすぐそこにあった!私の目の前で!それを指摘してくれてありがとう。 –

19

ボタンのターゲットを反復処理し、セレクタのコピーを別のボタンに作成する方法を示します。特定の例は、単にtouchupinsideイベントですが、それは通常私が使用しているすべてです。

for (id target in button.allTargets) { 
    NSArray *actions = [button actionsForTarget:target 
             forControlEvent:UIControlEventTouchUpInside]; 
    for (NSString *action in actions) { 
      [newButton addTarget:target action:NSSelectorFromString(action) forControlEvents:UIControlEventTouchUpInside]; 
    } 
} 
0

私は新しいものを割り当てる前に、すべての可能な不要なターゲット/アクションを削除するために、これを使用:

if let action = button.actions(forTarget: target, forControlEvent: .touchUpInside)?.first 
{ 
    button.removeTarget(target, action: NSSelectorFromString(action), for: .touchUpInside) 
} 

またはあなたが本当にすべてのアクション削除する場合:

if let actions = button.actions(forTarget: target, forControlEvent: .touchUpInside) 
{ 
    for action in actions 
    { 
     button.removeTarget(target, action: NSSelectorFromString(action), for: .touchUpInside) 
    } 
} 
関連する問題