我正在尝试以编程方式构建UI.如何让这个动作起作用?我正在与Swift一起开发.
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(); }
Dash.. 182
你只是在选择器名称的末尾错过了冒号.由于按下了一个参数,冒号必须在那里.您的被按下的函数也不应嵌套在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: #selector(myClass.pressed(_:)), forControlEvents: .TouchUpInside) self.view.addSubview(myFirstLabel) self.view.addSubview(myFirstButton) } @objc func pressed(sender: UIButton!) { var alertView = UIAlertView() alertView.addButtonWithTitle("Ok") alertView.title = "title" alertView.message = "message" alertView.show() }
编辑:更新以反映Swift 2.2中的最佳实践.应该使用#selector()而不是不推荐使用的文字字符串.
您应该能够通过访问UIButton的titleLabel属性以编程方式创建自定义UI按钮.
Swift中的每类引用:关于titleLabel属性,它表示"尽管此属性是只读的,但它自己的属性是可读/写的.主要使用这些属性来配置按钮的文本."
在Swift中,您可以直接修改titleLabel的属性,如:
let myFirstButton = UIButton() myFirstButton.titleLabel!.text = "I made a label on the screen #toogood4you" myFirstButton.titleLabel!.font = UIFont(name: "MarkerFelt-Thin", size: 45) myFirstButton.titleLabel!.textColor = UIColor.red myFirstButton.titleLabel!.textAlignment = .center myFirstButton.titleLabel!.numberOfLines = 5 myFirstButton.titleLabel!.frame = CGRect(x: 15, y: 54, width: 300, height: 500)
编辑
Swift 3.1语法
你只是在选择器名称的末尾错过了冒号.由于按下了一个参数,冒号必须在那里.您的被按下的函数也不应嵌套在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: #selector(myClass.pressed(_:)), forControlEvents: .TouchUpInside) self.view.addSubview(myFirstLabel) self.view.addSubview(myFirstButton) } @objc func pressed(sender: UIButton!) { var alertView = UIAlertView() alertView.addButtonWithTitle("Ok") alertView.title = "title" alertView.message = "message" alertView.show() }
编辑:更新以反映Swift 2.2中的最佳实践.应该使用#selector()而不是不推荐使用的文字字符串.
斯威夫特4
private func createButton { let sayButtonT = UIButton(type: .custom) sayButtonT.addTarget(self, action: #selector(sayAction(_:)), for: .touchUpInside) } @objc private func sayAction(_ sender: UIButton?) { }
斯威夫特3
let button = UIButton(frame: CGRect(x: 20, y: 20, width: 200, height: 60)) button.setTitle("Email", for: .normal) button.backgroundColor = .white button.setTitleColor(UIColor.black, for: .normal) button.addTarget(self, action: #selector(self.buttonTapped), for: .touchUpInside) myView.addSubview(button) @objc func buttonTapped(sender : UIButton) { //Write button action here }
尝试这些..我希望它有帮助......
override func viewDidLoad() { super.viewDidLoad() let btn = UIButton() btn.frame = CGRectMake(10, 10, 50, 50) //set frame btn.setTitle("btn", forState: .Normal) //set button title btn.setTitleColor(UIColor.redColor(), forState: .Normal) //set button title color btn.backgroundColor = UIColor.greenColor() //set button background color btn.tag = 1 // set button tag btn.addTarget(self, action: "btnclicked:", forControlEvents: .TouchUpInside) //add button action self.view.addSubview(btn) //add button in view }
这些是按钮点击事件..
func btnclicked(sender: UIButton!) { //write the task you want to perform on buttons click event.. }
是的在模拟器中.有时它不会识别选择器,看起来有一个错误.即使我不面对你的代码,我只是改变了动作名称(选择器).有用
let buttonPuzzle:UIButton = UIButton(frame: CGRectMake(100, 400, 100, 50)) buttonPuzzle.backgroundColor = UIColor.greenColor() buttonPuzzle.setTitle("Puzzle", forState: UIControlState.Normal) buttonPuzzle.addTarget(self, action: "buttonAction:", forControlEvents: UIControlEvents.TouchUpInside) buttonPuzzle.tag = 22; self.view.addSubview(buttonPuzzle)
这里有一个示例选择器函数:
func buttonAction(sender:UIButton!) { var btnsendtag:UIButton = sender if btnsendtag.tag == 22 { //println("Button tapped tag 22") } }
Swift 2.2 Xcode 7.3
由于现在不推荐使用Objective-C String Literals用于按钮回调方法
let button:UIButton = UIButton(frame: CGRectMake(100, 400, 100, 50)) button.backgroundColor = UIColor.blackColor() button.setTitle("Button", forState: UIControlState.Normal) button.addTarget(self, action:#selector(self.buttonClicked), forControlEvents: .TouchUpInside) self.view.addSubview(button) func buttonClicked() { print("Button Clicked") }
Swift 3 Xcode 8
let button:UIButton = UIButton(frame: CGRect(x: 100, y: 400, width: 100, height: 50)) button.backgroundColor = .black button.setTitle("Button", for: .normal) button.addTarget(self, action:#selector(self.buttonClicked), for: .touchUpInside) self.view.addSubview(button) func buttonClicked() { print("Button Clicked") }
Swift 4 Xcode 9
let button:UIButton = UIButton(frame: CGRect(x: 100, y: 400, width: 100, height: 50)) button.backgroundColor = .black button.setTitle("Button", for: .normal) button.addTarget(self, action:#selector(self.buttonClicked), for: .touchUpInside) self.view.addSubview(button) @objc func buttonClicked() { print("Button Clicked") }