ios 坚持以编程方式在 swift 3 中向按钮添加目标
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39815352/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Stuck with adding target to button in swift 3 programmatically
提问by user234159
I've created a UIButton using Swift 3 and I want it to print some message when it's pressed.
我已经使用 Swift 3 创建了一个 UIButton,我希望它在按下时打印一些消息。
So I did something like this: In loadView()
所以我做了这样的事情:在 loadView()
button.addTarget(self, action: #selector(ViewController.pressButton(button:)), for: .touchUpInside)
A method:
一个方法:
func pressButton(button: UIButton) {
NSLog("pressed!")
}
But nothing happens when I click the button.
但是当我点击按钮时没有任何反应。
回答by Rashwan L
Add the button code in your viewDidLoadand it will work for you:
在您的按钮中添加按钮代码,viewDidLoad它将为您工作:
override func viewDidLoad() {
super.viewDidLoad()
let button = UIButton(frame: CGRect(x: 10, y: 10, width: 100, height: 100))
button.backgroundColor = UIColor.gray
button.addTarget(self, action: #selector(pressButton(button:)), for: .touchUpInside)
self.view.addSubview(button)
}
func pressButton(button: UIButton) {
NSLog("pressed!")
}
You don′t need to add ViewController.pressButtonto selector, it′s enough with the function name.
你不需要添加ViewController.pressButton到selector,它就足够了function name。
Swift 4.x version:
斯威夫特 4.x 版本:
let button = UIButton(frame: CGRect(x: 10, y: 10, width: 100, height: 100))
button.backgroundColor = .gray
button.tag = 0
button.addTarget(self, action: #selector(pressButton(_:)), for: .touchUpInside)
self.view.addSubview(button)
@objc func pressButton(_ button: UIButton) {
print("Button with tag: \(button.tag) clicked!")
}
Swift 5.1 version:
斯威夫特 5.1 版本:
let button = UIButton(frame: CGRect(x: 10, y: 10, width: 100, height: 100))
button.backgroundColor = .gray
button.tag = 100
button.addTarget(self, action: #selector(pressButton), for: .touchUpInside)
self.view.addSubview(button)
@objc func pressButton(button: UIButton) {
print("Button with tag: \(button.tag) clicked!")
}
回答by Otis Lee
try this in Swift3!
在 Swift3 中试试这个!
button.addTarget(self, action: #selector(self.pressButton(button:)), for: .touchUpInside)
@objc func pressButton(button: UIButton) { NSLog("pressed!") }
回答by arango_86
Use the following code.
使用以下代码。
button.addTarget(self, action: #selector(FirstViewController.cartButtonHandler), for: .touchUpInside)
Your class name corresponds to FirstViewController
你的类名对应于FirstViewController
And your selector corresponds to the following function
并且您的选择器对应于以下功能
func cartButtonHandler() {
}
回答by Nikhil Manapure
In swift 3 use this -
在 swift 3 中使用这个 -
object?.addTarget(objectWhichHasMethod, action: #selector(classWhichHasMethod.yourMethod), for: someUIControlEvents)
For example(from my code) -
例如(来自我的代码)-
self.datePicker?.addTarget(self, action:#selector(InfoTableViewCell.datePickerValueChanged), for: .valueChanged)
Just give a :after method name if you want the sender as parameter.
:如果您希望发件人作为参数,只需在方法名称之后给出。
回答by BHendricks
You mention that the addTargetcall is in loadView(). Is this in your custom subview, of some kind, or the viewController?
您提到addTarget呼叫在loadView(). 这是在您的自定义子视图中,某种类型,还是视图控制器?
From your selector, it's targeting a method in your ViewControllerclass, but if the targetfor this action is the view itself, then it would make sense that the action is not going through.
从您的选择器来看,它的目标是您的ViewController类中的一个方法,但是如果target此操作是视图本身,那么该操作没有通过就有意义了。
If you declare your button in a viewController, and in viewDidLoadadd this target as above, then the message should be printed as you're looking for. I believe you are "targetting" the wrong class with your action.
如果您在 viewController 中声明您的按钮,并按viewDidLoad上述方式添加此目标,则应按您的要求打印消息。我相信您的行动“瞄准”了错误的班级。
回答by Divya Rayapati
let cancelButton = UIButton.init(frame: CGRect(x: popUpView.frame.size.width/2, y: popUpView.frame.size.height-20, width: 30, height: 30))
cancelButton.backgroundColor = UIColor.init(patternImage: UIImage(named: cancelImage)!)
cancelButton.addTarget(self, action: #selector(CommentsViewController.canceled), for:.touchUpInside)
回答by SpkingR
- Add the button code in
override func viewDidLoad()method Make sure your action handler tagged with @IBAction like this:
@IBAction func pressButton(button: UIButton) { print("pressed!") }
- 在
override func viewDidLoad()方法中添加按钮代码 确保你的动作处理程序像这样用@IBAction 标记:
@IBAction func pressButton(button: UIButton) { print("pressed!") }
Then it will work!
然后它会起作用!

