2015-11-24 135 views
1

我想添加按鈕到編程方式創建的視圖,我添加到主視圖。我無法採取行動來處理按鈕。這是我試過到目前爲止:按鈕點擊Swift將按鈕添加到視圖編程方式與動作

itemView = UIImageView(frame:CGRect(x:0, y:0, width:240, height:375)) 
itemView.backgroundColor = UIColor(red: 255.0/0.0, green: 255.0/0.0, blue: 255.0/0.0, alpha: 0.05) 
itemView.contentMode = .Center 

buttonConnect = UIButton(frame: CGRect(x:0, y:335, width:120, height:40)) 
buttonConnect.backgroundColor = UIColor(red: 0.8, green: 0.6, blue: 0.2, alpha: 1.0) 
buttonConnect.setTitle("Connect", forState: .Normal) 
buttonConnect.tag = 3 
buttonConnect.addTarget(itemView, action: "btnConnectTouched:", forControlEvents:.TouchDown) 
itemView.addSubview(buttonConnect) 

self.addSubview(itemView) 

方法是這樣的:

func btnConnectTouched(sender:UIButton!) 
{ 
    print("button connect touched") 
} 

可有人建議這是什麼問題?謝謝!

回答

5

我不明白你爲什麼要在UIImageView中添加一個按鈕。即使這是你所需要的,帶一個UIView,然後把所有其他控件放在裏面。你的按鈕沒有被點擊的原因是你已經把它放在UIImageView和UIImageView中,userInteractionEnabled的默認值是false。所以你需要啓用它,即

itemView.userInteractionEnabled = true; 

還設置按鈕目標爲'self'而不是itemView。

+0

這是一個很好的答案。非常感謝!我將'UIImageView'改爲'UIView',並且將目標改爲'self',並且它可以工作 – bla0009

1

你錯在目標:

// buttonConnect.addTarget(itemView, action: "btnConnectTouched:", forControlEvents:.TouchDown) 
    // using 
    buttonConnect.addTarget(self, action: "btnConnectTouched:", forControlEvents:.TouchDown) 
    // or buttonConnect.addTarget(self, action: "btnConnectTouched:", forControlEvents:.TouchUpInside) 
+0

我嘗試這樣做,它仍然沒有工作。 – bla0009

+0

@ bla0009是否將'itemView'更改爲'self'? – anhtu

+0

@anthu是的,仍然沒有發生按鈕點擊 – bla0009

1
override func viewDidLoad() { 
    super.viewDidLoad() 
    // Do any additional setup after loading the view, typically from a nib. 

    let buttonConnect = UIButton()  
    buttonConnect = UIButton(frame: CGRect(x:0, y:335, width:120, height:40)) 
    buttonConnect.backgroundColor = UIColor(red: 0.8, green: 0.6, blue: 0.2, alpha: 1.0) 
    buttonConnect.setTitle("Connect", forState: .Normal) 
    buttonConnect.tag = 3 
    buttonConnect.addTarget(self, action: "btnConnectTouched:", forControlEvents: .TouchUpInside)  
    self.view.addSubview(buttonConnect) 
} 

func btnConnectTouched(sender: UIButton!) { 
    print("button connect touched"); 
} 
+0

更改forControlEvents:.TouchDown爲forControlEvents:.TouchUpInside –

2

嘗試改變tarjet和事件

buttonConnect.addTarget(itemView, action: "btnConnectTouched:", forControlEvents:.TouchDown) 

buttonConnect.addTarget(self, action: "btnConnectTouched:", forControlEvents:. TouchUpInside) 
+0

我試過這個,它仍然無法正常工作。 – bla0009

0

嘗試buttonConnect.addTarget(itemView, action: "btnConnectTouched", forControlEvents:.TouchUpInside)沒有:

0

如果您可以在代碼中進行以下兩項更改,它將起作用。

  1. 替換用的UIImageView爲UIView的ItemView控件類型和
  2. 與self.view.addSubview更換self.addSubview(ItemView控件)(ItemView控件)

你的最終代碼看起來應該像....

override func viewDidLoad() { 
    super.viewDidLoad() 
    // Do any additional setup after loading the view, typically from a nib. 

    var itemView = UIView(frame:CGRect(x:0, y:0, width:240, height:375)) 
    itemView.backgroundColor = UIColor(red: 255.0/0.0, green: 255.0/0.0, blue: 255.0/0.0, alpha: 0.05) 
    itemView.contentMode = .Center 

    var buttonConnect = UIButton(frame: CGRect(x:0, y:335, width:120, height:40)) 
    buttonConnect.backgroundColor = UIColor(red: 0.8, green: 0.6, blue: 0.2, alpha: 1.0) 
    buttonConnect.setTitle("Connect", forState: .Normal) 
    buttonConnect.tag = 3 
    buttonConnect.addTarget(self, action: "btnConnectTouched:", forControlEvents:.TouchDown) 


    itemView.addSubview(buttonConnect) 

    self.view.addSubview(itemView) 

} 

func btnConnectTouched(sender:UIButton!) 
{ 
    print("button connect touched") 
} 

試試這個....它的工作對我來說....

相關問題