我正尝试以编程方式构建UI。我如何得到这个动作?我正在用Swift开发。在Swift中以编程方式制作UIButton
代码在viewDidLoad中:
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let myFirstLabel = UILabel()
let myFirstButton = UIButton()
myFirstLabel.text = "I made a label on the screen #toogood4you"
myFirstLabel.font = UIFont(name: "MarkerFelt-Thin", size: 45)
myFirstLabel.textColor = UIColor.redColor()
myFirstLabel.textAlignment = .Center
myFirstLabel.numberOfLines = 5
myFirstLabel.frame = CGRectMake(15, 54, 300, 500)
myFirstButton.setTitle("✸", forState: .Normal)
myFirstButton.setTitleColor(UIColor.blueColor(), forState: .Normal)
myFirstButton.frame = CGRectMake(15, -50, 300, 500)
myFirstButton.addTarget(self, action: "pressed", forControlEvents: .TouchUpInside)
self.view.addSubview(myFirstLabel)
self.view.addSubview(myFirstButton)
}
func pressed(sender: UIButton!) {
var alertView = UIAlertView();
alertView.addButtonWithTitle("Ok");
alertView.title = "title";
alertView.message = "message";
alertView.show();
}
当我按下按钮时,该应用程序仍然崩溃。 – Benr783
对不起。该功能不应嵌套。 – Dash
嘿,你能告诉我冒号后面的逻辑,我们需要在代表动作的字符串后添加吗? – juniorgarcia