UIButton action generates a SIGABRT but no breakpoint triggers - swift

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")
}
}

Related

How do I targeting selector with addTarget in another class

I try to understand how to control component form one class to another class but that's not working.
class VC:UIViewController {
override func viewDidLoad() {
let instance = Test()
self.view.addSubView(instance.button)
}
}
class Test:NSObject {
var button:UIButton!
override init() {
button = UIButton(frame:CGRect(x:0, y:0, width:100, height:40))
button.setTitle(("TEST", for:.normal)
button.addTarget(self, action: #selector(tapButton(_:)), for: .touchUpInside)
}
#objc func tapButton(_ sender:UIButton) {
print("TAP Button")
}
}
When I tapped the button, nothing happens !
I try to change
button.addTarget(self, action: #selector(tapButton(_:)), for: .touchUpInside)
with
button.addTarget(nil, action: #selector(tapButton(_:)), for: .touchUpInside)
That's not working !
How to resolve this problem ?
Thanks for your help.
You have 3 issues:
instance is a local variable, so it gets deallocated as soon as viewDidLoad() finishes. Make it a property of VC. The button only keeps a weak reference to the object, so when it is deallocated it becomes nil. In cases where the target is nil, UIKit will search up the responder chain for the action method. Since VC doesn't supply a tapButton method, nothing happens when the button is pressed.
You need to call super.init() so that self is available to be used with the button. self can't be created until all properties have been initialized. Because init is an override, you must call super.init() to initialize the properties that NSObject provides before you can use self in the button.
Your frame puts your button in an unaccessible region of the screen. I changed your frame to put the button inside the safe area of the iPhone 11. I also made the button .green so that I could see it.
class VC: UIViewController {
var instance = Test() // make instance a property
override func viewDidLoad() {
// let instance = Test() // this was a local variable that doesn't hang around
self.view.addSubview(instance.button)
}
}
class Test: NSObject {
var button: UIButton!
override init() {
super.init() // call this so that you can use self below
button = UIButton(frame:CGRect(x: 100, y: 100, width: 100, height: 40))
button.setTitle("TEST", for:.normal)
button.backgroundColor = .green
button.addTarget(self, action: #selector(tapButton(_:)), for: .touchUpInside)
}
#objc func tapButton(_ sender: UIButton) {
print("TAP Button")
}
// Add deinit to see when this object is deinitialized. When
// instance is local to viewDidLoad() this object gets freed
// when viewDidLoad() finishes.
deinit {
print("Oops, the Test object has been deinitialized")
}
}

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)

How can I call a function that creates a button from another viewcontroller?

I have a function in my TuesdayViewController.swift that creates a button.
Whenever it's Tuesday, I want that button to appear in my ViewController.swift
I have this function in my TuesdayViewController:
func tuesdayView(){
let button = UIButton(frame: CGRect(x: 16, y: 200, width: 343, height: 45))
button.backgroundColor = .red
button.setTitle("Test button", for: .normal)
self.view.addSubview(button)
}
If I call this function in Tuesday's viewDidLoad(), the button shows in the TuesdayViewController.
How can I make it so I call this function through my main ViewController and for the button to show in the main ViewController screen?
I have tried this in my main ViewController but the button doesn't appear:
override func viewDidLoad(){
super.viewDidLoad()
TuesdayViewController().tuesdayVew()
}
Any help would be appreciated!
If I understand your question right, you just want to add the same kind of button from your TuesdayViewController into your MainViewController, and not adding the button to the TuesdayViewController by calling it in the MainViewController.
To do that, you just can declare your tuesdayView as a public property of your TuesdayViewController, like so:
TuesdayViewController.swift
class TuesdayViewController: UIViewController {
public lazy var tuesdayView: UIButton = {
let button = UIButton(frame: CGRect(x: 16, y: 200, width: 343, height: 45))
button.backgroundColor = .red
button.setTitle("Test button", for: .normal)
return button
}()
override func viewDidLoad(){
super.viewDidLoad()
self.view.addSubview(self.tuesdayView)
}
}
And call that property like what you've been doing before.
MainViewController.swift
class MainViewController: UIViewController {
override func viewDidLoad(){
super.viewDidLoad()
let button = TuesdayViewController().tuesdayView
self.view.addSubview(button)
}
}
You need to use an observer with NotificationCenter:
First you need to create a Notification Name
extension Notification.Name {
static let notName = Notification.Name("notName")
}
TuesdayViewController:
override func viewDidLoad() {
NotificationCenter.default.addObserver(self, selector: #selector(tuesdayViewObserver), name: .notName, object: nil)
}
#objc func tuesdayViewObserver(_ notification: Notification) {
//Here you call the tuesdayView function
tuesdayView()
}
And in the controller in which you want to fire the method, you post the notification and the method will be executed:
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.post(name: .notName, object: nil)
}
For further info, I recommend you to take a look to NotificationCenter and how it works. LINK

Swift 3 Method from Class not working with #selector syntax

I feel as though I'm not understanding the Swift #selectors properly. I'm trying to connect a button to a method from another class.
I have a class to print the button when pushed:
class printThings {
#IBAction func printMe(_ sender: UIButton){
print("Button Pushed.")
}
}
And then the ViewController:
class ViewController : UIViewController {
override func ViewDidLoad(){
super.viewDidLoad()
//button setup here
let printMe = printThings()
button.addTarget(printMe, action: #selector(printMe.printMe(_:)), for: .touchUpInside)
//add button to subview
}
}
This never triggers the print statement in the class. I'm sure I'm missing something simple.
Thanks.
The problem is that printMe is a temporary, local variable:
let printMe = printThings() // local variable
button.addTarget(printMe, action: #selector(printMe.printMe(_:)), for: .touchUpInside)
// ... and then viewDidLoad ends, and `printMe` vanishes
So by the time you push the button, printMe has vanished; there is no one to send the button message to.
If you want this to work, you need to make printMe persist:
class ViewController : UIViewController {
let printMe = printThings() // now it's a _property_ and will persist
override func viewDidLoad(){
super.viewDidLoad()
//button setup here
button.addTarget(self.printMe, action: #selector(self.printMe.printMe(_:)), for: .touchUpInside)
//add button to subview
}
}

Why is my UIButton's target never reached?

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.