Why is my UIButton's target never reached? - swift

func setupNewBinderButton() {
let binderButton = UIButton.buttonWithType(UIButtonType.Custom) as UIButton
binderButton.frame = CGRectMake(50, 384-25, 50, 50)
binderButton.setTitle("Add", forState: UIControlState.Normal)
binderButton.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
binderButton.addTarget(self, action: "addBinderTapped", forControlEvents: UIControlEvents.TouchUpInside)
binderButton.userInteractionEnabled = true
self.view.addSubview(binderButton)
}
func addBinderTapped() {
println("hi")
abort()
}
override func viewDidLoad() {
super.viewDidLoad()
setupNewBinderButton()
The breakpoint I set in this function doesn't get hit. The log doesn't get hit. The abort doesn't happen.
How do I make my UIButton respond to touches in Swift?

Use like this as it needs to be tied to IBAction
#IBAction func addBinderTapped(sender: UIButton)
{
}
Connect this action with your button by command+drag to storyboard button to tie this function.

Related

Cannot attach existing method to UIButton

I'm attempting to make a mock-up application, but I cannot seem to get the UIButton to hook up to the action that should be executed when I click it.
I'm not sure why it cannot find the method, the names are the same and adding in a (_:) to the #selector does nothing.
import UIKit
class LoginViewController: UIViewController {
var loginButton: UIButton?
#objc func onLoginButtonPress(sender: UIButton!) {
print("Login Button Press")
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
setup()
}
func setup() -> Void {
initLoginButton()
self.view.backgroundColor = .green
}
func initLoginButton() -> Void {
let loginButtonBounds = CGRect(x: 100, y: 100, width: 250, height: 250)
loginButton = UIButton(frame: loginButtonBounds)
loginButton!.backgroundColor = .red
loginButton!.setTitle("Login", for: .normal)
loginButton!.addTarget(self, action: #selector("onLoginButtonPress"), for: .touchUpInside)
self.view.addSubview(loginButton!)
}
loginButton!.addTarget(self, action: #selector(onLoginButtonPress(sender:)), for: .touchUpInside)
edit this line code, this should work for you. Its better to not use force-unwrapping so avoid using it.
Have you tried removing the parenthesis from "onLoginButtonPress"? I don't think they are needed.
loginButton!.addTarget(self, action: #selector("onLoginButtonPress"), for: .touchUpInside)

Creating a checkbox programmatically using images as the UIButtons background

I have seen multiple questions on how to implement a checkbox by just changing the background image when clicked, but I don't understand why the checkbox only shows when I add it to my ViewController setupViews.
It simply will not show up or change when I have all the functionality in my actionButton function. Should i be using a protocol and delegate set up to get my button showing changing when clicked? Below is my code, I am hoping someone can shed some light as to what I am missing here?
class MainMenuViewController: UIViewController {
let clickingCheckbox = ClickingCheckbox()
var checkbox = UIImage(named: "Checked_Checkbox")
var empty_checkbox = UIImage(named:"Empty_Checkbox")
var isBoxClicked: Bool!
override func viewDidLoad() {
super.viewDidLoad()
setupViews()
}
func setupViews() {
self.backgroundImage.addSubview(contentView)
self.contentView.addSubview(clickingCheckbox)
clickingCheckbox.snp.makeConstraints { (make) in
make.top.equalTo(signInButton.snp.bottom).offset(-MainMenuViewController.padding)
make.leading.equalTo(buttonView)
make.width.equalTo(signInButton).multipliedBy(0.2)
make.height.equalTo(clickingCheckbox.snp.width)
}
clickingCheckbox.addTarget(self, action: #selector(buttonAction(_:)), for: .touchUpInside)
clickingCheckbox.setImage(empty_checkbox, for: UIControlState.normal) #The checkbox only shows on screen if I put it here, however it does nothing when clicked!
}
#objc func buttonAction(_ sender: ClickingCheckbox) {
if isBoxClicked == true {
isBoxClicked = false
}else{
isBoxClicked = true
}
if isBoxClicked == true {
sender.setImage(checkbox, for: UIControlState.selected)
}else{
sender.setImage(empty_checkbox, for: UIControlState.normal)
}
print("test")
}
In my Class I have.....
class ClickingCheckbox: UIButton {
override func draw(_ rect: CGRect) {
super.draw(rect)
}
}
I have tried keeping the buttonAction functionality in the class, but that didn't work, I have tried a multiple different ways but can't get my head around how to show it working. All other advice is given to implement using IBOutlets so this would be really helpful for me to understand. Thanks
Try something like this:
class ClickingCheckbox: UIButton {
convenience init() {
self.init(frame: .zero)
self.setImage(UIImage(named: "Empty_Checkbox"), for: .normal)
self.setImage(UIImage(named: "Checked_Checkbox"), for: .selected)
self.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside)
}
#objc private func buttonTapped() {
self.isSelected = !self.isSelected
}
}

UIButton action generates a SIGABRT but no breakpoint triggers

I have this code to create a button on my storyboard
override func viewDidLoad() {
super.viewDidLoad()
...
signupButton.addTarget(self, action: "pressed:", forControlEvents: .TouchUpInside)
...
}
func pressed(sender:UIButton!) {
println("Pressed")
}
However, it throws a SIGABRT error when I press the button. So, I added a breakpoint on all exceptions. However, no breakpoint was ever it - it just threw the exception. How do I find out what's causing the error?
You have two issues here...
First: You placed the pressed function in the scope of viewDidLoad, when it needs to be outside of that function and in the scope of the class. Move the function pressed out of viewDidLoad().
Second: This is not what is causein the signal abort crash, but it is needed nonetheless. Add a semicolon to the action selector name.
signupButton.addTarget(self, action: "pressed:", forControlEvents: .TouchUpInside)
Or, if you don't need to pass any object to the function pressed, then change the function header to func pressed().
BASIC EXAMPLE: (tested and working in new project)
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let signupButton = UIButton(frame: CGRectMake(0, 0, 100.0, 100.0))
signupButton.setTitle("BUTTON", forState: UIControlState.Normal)
signupButton.addTarget(self, action: "pressed:", forControlEvents: .TouchUpInside)
self.view.addSubview(signupButton)
}
func pressed(sender:UIButton!) {
println("Button pressed")
}
}

How to highlight the background of a UIButton when it is clicked and "unhighlight" it?

I am trying to create 4 UIButtons that highlight and stay highlighted when they are clicked. The only problem is I need only one UIButton to be Highlighted at a time. So, if there is a UIButton highlighted already, I need it to be "unhighlighted" and highlight the UIButton I clicked. I have tried to do this before and failed. Please help me with this problem.
I am using the Swift coding language to do this.
Any input or suggestions would be greatly appreciated.
If you give this answer an upvote, remember to upvote dasblikenlight's answer as well.
class ViewController: UIViewController {
// Connect all 4 buttons to this outlet
#IBOutlet var radioGroup: [UIButton]!
// Connect this action to all 4 buttons
#IBAction func radioGroupClicked(sender: AnyObject) {
// Unhighlight all buttons
unhighlightRadioGroup()
// Highlight the one being clicked on
highlightRadioGroup(sender as! UIButton)
}
// Set all 4 buttons in unselected state
func unhighlightRadioGroup() {
for button in radioGroup {
button.selected = false
}
}
// Set one button in the selected state
func highlightRadioGroup(button : UIButton) {
button.selected = true
}
}
You can do it with an IBOutletCollection. Command-drag one of the buttons into the view controller code, and choose creating of an IBOutletCollection on drop, and name your collection something - say, radioGroup. Then control-drag the remaining three buttons into the same IBOutletCollection.
Next thing is to add a method to un-highlight all buttons in your radioGroup. This can be done with a simple loop.
Finally, add calls to unhighlightRadioGroup from the event handler of your buttons. Event handler should first call your unhighlightRadioGroup method, and then highlight the sender received in the event handler.
lazy var buttonsArray: [UIButton] = {
var buttons = [UIButton]()
let firstButton = UIButton()
let secondButton = UIButton()
let thirdButton = UIButton()
let fourthButton = UIButton()
buttons = [firstButton, secondButton, thirdButton, fourthButton]
return buttons
}()
private func setupButtonMethods() {
filteredButtons[0].addTarget(self, action: #selector(firstButtonPressed(sender:)), for: .touchUpInside)
filteredButtons[1].addTarget(self, action: #selector(secondButtonPressed(sender:)), for: .touchUpInside)
filteredButtons[2].addTarget(self, action: #selector(thirdButtonPressed(sender:)), for: .touchUpInside)
filteredButtons[3].addTarget(self, action: #selector(fourthButtonPressed(sender:)), for: .touchUpInside)
}
private func setupHiglightedStateOnButton(button: UIButton) {
for btn in buttonsArray {
btn.isSelected = false
btn.backgroundColor = .gray
btn.setTitleColor(.white, for: .normal)
btn.isUserInteractionEnabled = true
}
button.isSelected = true
button.backgroundColor = .yellow
button.setTitleColor(.black, for: .normal)
button.isUserInteractionEnabled = false
}
#objc func firstButtonPressed(sender: UIButton) {
setupHiglightedStateOnButton(button: sender)
}
#objc func secondButtonPressed(sender: UIButton) {
setupHiglightedStateOnButton(button: sender)
}
#objc func thirdButtonPressed(sender: UIButton) {
setupHiglightedStateOnButton(button: sender)
}
#objc func fourthButtonPressed(sender: UIButton) {
setupHiglightedStateOnButton(button: sender)
}
remember to call setupButtonMethods() inside viewDidLoad()

Swift Reload textfield from button action

I have a button that creates many more buttons with a textfield:
func CreateButtonWithIndex(index:Int) {
let newButton = UIButton.buttonWithType(UIButtonType.System) as UIButton
newButton.setTitle(“Button”, forState: UIControlState.Normal)
newButton.addTarget(self, action: Selector("go:"), forControlEvents: UIControlEvents.TouchUpInside)
newButton.tag = index+1;
var textFieldButton: UITextField = UITextField()
textFieldButton.tag = index+1;
textFieldButton.text = “textfield”
self.view.addSubview(textFieldButton)
self.view.addSubview(newButton)
}
func go(sender: AnyObject) {
println(“ok press button“)
}
How can I detect the textfield corresponding to the button pressed?
convert the sender into a UIButton
#IBAction func go(sender: AnyObject) {
let yourButton = sender as UIButton
println(yourButton.titleLabel.text)
}
Even better, if you're certain that only buttons are going to call the "go" action, you could instead do this:
#IBAction func go(sender: UIButton) {
println(sender.titleLabel.text)
if(sender.titleLabel.text == "OK")
println("OK button was pressed")
}
And more information can be found in the "Comparing Strings" section of Apple's Strings & Characters Swift documentation and the #IBOutlet thing I found in this tutorial.
Subclass UIButton. Add a UITextField variable to the subclassed UIButton. Assign the new UITextField variable after you create the UIButton subclass.
UIButton subclass:
class TextFieldButton: UIButton {
var textField: UITextField! // use this to reference the related text field
}
The code in your other class:
func CreateButtonWithIndex(index:Int) {
let newButton = UIButton.buttonWithType(UIButtonType.System) as TextFieldButton // use our new subclass
newButton.setTitle(“Button”, forState: UIControlState.Normal)
newButton.addTarget(self, action: Selector("go:"), forControlEvents: UIControlEvents.TouchUpInside)
newButton.tag = index+1;
var textFieldButton: UITextField = UITextField()
textFieldButton.tag = index+1;
textFieldButton.text = "textfield"
newButton.textField = textFieldButton // set reference to the text field
self.view.addSubview(textFieldButton)
self.view.addSubview(newButton)
}
func go(sender: AnyObject) {
println("ok press button")
println(sender.textField) // now we can access the related text field
}