2017-04-15 42 views
0

我想建立一個自定義控制器來管理一組UIButtons。不能在另一個類中設置UIButton目標?

的ViewController其中按鈕是安裝和放置:

override func viewDidLoad() { 
    super.viewDidLoad() 

    let button1 = UIButton() 
    button1.setTitle("Button 1", for: UIControlState.normal) 
    button1.setTitleColor(UIColor.blue, for: UIControlState.normal) 
    button1.frame.origin.y = 120 
    button1.sizeToFit() 
    button1.isUserInteractionEnabled = true 

    let button2 = UIButton() 
    button2.setTitle("Button 2", for: UIControlState.normal) 
    button2.setTitleColor(UIColor.blue, for: UIControlState.normal) 
    button2.frame.origin.y = 160 
    button2.sizeToFit() 


    let button3 = UIButton() 
    button3.setTitle("Button 3", for: UIControlState.normal) 
    button3.setTitleColor(UIColor.blue, for: UIControlState.normal) 
    button3.frame.origin.y = 190 
    button3.sizeToFit() 

    let controller = CustomButtonController(buttons: button1, button2, button3) 

    self.view.addSubview(button1) 
    self.view.addSubview(button2) 
    self.view.addSubview(button3) } 

CustomButtonController:

class CustomButtonController : NSObject { 
    init(buttons: UIButton...) { 
     super.init() 
     for aButton in buttons { 
      aButton.addTarget(self, action: #selector(pressed(_:)), for: UIControlEvents.touchUpInside) 
     } 
    } 

    func pressed(_ sender: UIButton) { 
     print("Press received in CustomButtonController") 
     //Never gets called 
    } 
} 

如果我使用addTarget方法上的按鈕從內部的ViewController,然後正常工作。任何想法爲什麼我們不能像這樣將目標添加到已傳遞的UIButton?謝謝

+0

的事抓我的眼睛被繼承NSObject的。根據情況,我傾向於採用兩種不同的方式。 (1)如果我想創建一個按鈕並在它的init上添加一個目標,我創建一個*方便初始值設定項*並傳入* target *和* selector *。 (2)如果我想傳遞按鈕被按下的事實,我bot(a)標記它並(b)使用按鈕的* .sendAction *代替。也許其中一種方法可以爲你工作。 – dfd

+0

魔鬼在細節中。您需要顯示調用此操作的特定調用序列。哪個對象創建誰?誰在調用CustomButtonController?我的猜測是你在視圖控制器的視圖加載之前調用了CustomButtonController。 –

+0

@DuncanC添加init代碼,不要認爲按鈕init有任何問題。對象被初始化並傳遞正常,CustomButtonController在每個按鈕上運行addTarget()。它只是不調用選擇器。 – user339946

回答

2

您正在創建CustomButtonController作爲viewDidLoad的本地常量。一旦viewDidLoad返回,這個對象被釋放,因此不再處理按鈕水龍頭。

您需要聲明實例屬性來保存控制器實例:

var controller: CustomButtonController! 

override func viewDidLoad() { 
    super.viewDidLoad() 

    let button1 = UIButton() 
    button1.setTitle("Button 1", for: UIControlState.normal) 
    button1.setTitleColor(UIColor.blue, for: UIControlState.normal) 
    button1.frame.origin.y = 120 
    button1.sizeToFit() 
    button1.isUserInteractionEnabled = true 

    let button2 = UIButton() 
    button2.setTitle("Button 2", for: UIControlState.normal) 
    button2.setTitleColor(UIColor.blue, for: UIControlState.normal) 
    button2.frame.origin.y = 160 
    button2.sizeToFit() 


    let button3 = UIButton() 
    button3.setTitle("Button 3", for: UIControlState.normal) 
    button3.setTitleColor(UIColor.blue, for: UIControlState.normal) 
    button3.frame.origin.y = 190 
    button3.sizeToFit() 

    controller = CustomButtonController(buttons: button1, button2, button3) 

    self.view.addSubview(button1) 
    self.view.addSubview(button2) 
    self.view.addSubview(button3) 
} 
+0

是的,我正要說同樣的事情,但你打敗了我。 (投票。)如果你不保留對你的'CustomButtonController'的強引用,它將被釋放。 –