[英]Swift adding button into view programatically with action
我正在嘗試將按鈕添加到我添加到主視圖的以編程方式創建的視圖中。 我無法采取任何措施來操作按鈕。 這是我到目前為止嘗試過的:
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")
}
有人可以建議出什么問題嗎? 謝謝!
我不明白為什么要在UIImageView中添加按鈕。 甚至這就是您所需要的,使用UIView,然后將所有其他控件放入其中。 您的按鈕未單擊的原因是您將其放置在UIImageView中,並且對於UIImageView,userInteractionEnabled的默認值為false。 所以你需要啟用它
itemView.userInteractionEnabled = true;
還將按鈕目標設置為“ self”,而不是itemView。
嘗試更改tarjet和事件
buttonConnect.addTarget(itemView, action: "btnConnectTouched:", forControlEvents:.TouchDown)
進入
buttonConnect.addTarget(self, action: "btnConnectTouched:", forControlEvents:. TouchUpInside)
您在目標上錯了:
// buttonConnect.addTarget(itemView, action: "btnConnectTouched:", forControlEvents:.TouchDown)
// using
buttonConnect.addTarget(self, action: "btnConnectTouched:", forControlEvents:.TouchDown)
// or buttonConnect.addTarget(self, action: "btnConnectTouched:", forControlEvents:.TouchUpInside)
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");
}
嘗試buttonConnect.addTarget(itemView, action: "btnConnectTouched", forControlEvents:.TouchUpInside)
在"btnConnectTouched"
之后不帶:
如果您可以在代碼中進行以下兩項更改,則它將起作用。
您的最終代碼應如下所示:...
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")
}
試試這個...。它對我有用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.