#IBAction crashes app in Swift - ios

I have button and method
switchViewButton = UIButton.buttonWithType(.System) as? UIButton
switchViewButton!.frame = CGRectMake(15, 25, 50, 50)
switchViewButton!.setTitle("sss", forState: .Normal)
switchViewButton!.addTarget(self, action: "switchViewFunc", forControlEvents: .TouchUpInside)
switchViewButton!.backgroundColor = UIColor.redColor()
switchViewButton!.layer.cornerRadius = 25
topView.addSubview(switchViewButton)
#IBAction func switchViewFunc(sender : AnyObject){
println("Button was clicked", sender)}
This method crashes app with reason "unrecognized selector sent to instance"
I have another button and method just like this, and it's work perfect. If I'm delete (sender : AnyObject) it works. I cann't understand what is wrong

Add action this way (pay attention to ":"
switchViewButton!.addTarget(self, action: "switchViewFunc:", forControlEvents: .TouchUpInside)

#IBAction func switchViewFunc(sender : AnyObject){
println("Button was clicked", sender)} - ?????
Try this:
func switchViewFunc(sender : UIButton!){
println("Button was clicked", sender)
}

#IBAction and an argument are not needed.
func switchViewFunc(){
println("Button was clicked", sender)
}

Related

addTarget to UIButton programmatically gives me 'unrecognized selector sent to class'

I'm trying to learn how to create button target actions, however, when I press the button, I get those LLDB errors and I get told that it was an 'unrecognized selector sent to class'.
Where am I going wrong here?
StatusCell.swift:
let phoneIcon: UIButton = {
let iv = UIImageView()
iv.translatesAutoresizingMaskIntoConstraints = false
iv.image = UIImage(named: "Phone3")?.imageWithRenderingMode(.AlwaysTemplate)
let phoneBtn = UIButton(type: .Custom)
phoneBtn.addTarget(CallButton.self, action: #selector(CallButton.buttonPressed(_:)), forControlEvents: .TouchDown)
phoneBtn.addTarget(CallButton.self, action: #selector(CallButton.buttonReleased(_:)), forControlEvents: .TouchUpInside)
phoneBtn.translatesAutoresizingMaskIntoConstraints = false
phoneBtn.setImage(iv.image!, forState: .Normal)
phoneBtn.tintColor = UIColor(r: 224, g: 224, b: 224)
return phoneBtn
}()
Here's the CallButton class where I call for buttonPressed and buttonReleased.
class CallButton: UIControl {
func buttonPressed(sender: AnyObject?) {
print("Pressed")
}
func buttonReleased(sender: AnyObject?) {
print("Let go")
}
}
The value for parameter target must be an instance of CallButton, not the type itself.
You are setting the class itself, not an instance, as the target of the action.
Therefore, the method you set as the action should be implemented as a class method, not an instance method:
class func buttonPressed(sender: AnyObject?) {
print("Pressed")
}

Why do I get signal SIGABRT when I select a created button?

I'm creating a button in swift 2 and when I select it, I get signal SIGABRT and the app crashes. Heres the code:
let button = UIButton()//(type: UIButtonType.System) as UIButton!
button.setTitle("button", forState: .Normal)
button.setTitleColor(UIColor.blueColor(), forState: .Normal)
button.addTarget(self, action: "buttonPressed:", forControlEvents: UIControlEvents.TouchUpInside)
button.frame = CGRectMake(100, 100, 100, 100)
self.view.addSubview(button)
func buttonPressed(sender: UIButton!) {
print("ButtonIsSelected")
}
It brings me to AppDelegate.swift and in the middle of the NSLog it says: unrecognized selector sent to instance...
Please help. Anton
func buttonPressed(sender: UIButton!) {
print("ButtonIsSelected")
}
This method must be in your class body not in function body . As I guess you have done.

Select/deselect buttons swift xcode 7

Part way done with learning swift but I hit a small wall and yet again, I'm sure I'm just a bit new at this and an easy solution is there but I'm having trouble figuring out how to select/deselect buttons below is what I have so far and it is a button turns into a checkmark when clicked on... I've gotten that far but I need that button to deselect when clicked on again and then obviously be able to be clicked again if need be.
#IBAction func buttonPressed(sender: AnyObject) {
sender.setImage(UIImage(named: "Checkmark.png"), forState: .Normal)
}
Swift 3 note: .selected and .checked are now lower case UIControlState values in the SDK, and some of the methods have been renamed:
let button = UIButton()
button.setImage(UIImage(named: "Unchecked"), for: .normal)
button.setImage(UIImage(named: "Checked"), for: .selected)
You can also now use image literals with Xcode 8 instead of UIImage(named:):
#imageLiteral(resourceName: "Unchecked")
Swift 2:
Why not use the .Selected state of the button as the "checked" state, and the .Normal state as the "unchecked" state.
let button = UIButton()
button.setImage(UIImage(named: "Unchecked"), forState: .Normal)
button.setImage(UIImage(named: "Checked"), forState: .Selected)
// ...
#IBAction func buttonPressed(sender: AnyObject) {
if let button = sender as? UIButton {
if button.selected {
// set deselected
button.selected = false
} else {
// set selected
button.selected = true
}
}
}
You dont need to set selected in condition. I just doing with following method in swift:
func selectDeselect(sender: UIButton){
sender.selected = !sender.selected
if(sender.selected == true)
{
sender.setImage(UIImage(named:"select_heart"), forState: UIControlState.Normal)
}
else
{
sender.setImage(UIImage(named:"heart"), forState: UIControlState.Normal)
}
}
Here is Working code for swift 4.
Make Sure you need to connect Button IBAction Outlet as UIButton and set default button image from storyboard whatever you want.
#IBAction func btnTapped(_ sender: UIButton) {
if sender.currentImage == UIImage(named: "radio_unchecked"){
sender.setImage(UIImage(named: "radio_checked"), for: .normal)
}else{
sender.setImage(UIImage(named: "radio_unchecked"), for: .normal)
}
}
update for Swift 4+ :
let button = UIButton()
button.setImage(UIImage(named: "Unchecked"), forState: .Normal)
button.setImage(UIImage(named: "Checked"), forState: .Selected)
#IBAction func buttonPressed(sender: AnyObject) {
if let button = sender {
if button.isSelected {
// set deselected
button.isSelected = false
} else {
// set selected
button.isSelected = true
}
}
}
Set uncheck image on default state from storyboard and check image on selected state from storyboard.
#IBAction func buttonPressed(sender: AnyObject) {
buttonOutlet.isSelected = !buttonOutlet.isSelected
}
private(set) lazy var myButton: UIButton = {
let button = UIButton()
button.setImage(UIImage(named: "Unchecked"), for: .normal)
button.setImage(UIImage(named: "Checked"), for: .selected)
button.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside)
button.translatesAutoresizingMaskIntoConstraints = false
return button
}()
#objc
func buttonTapped(sender: AnyObject) {
sender.isSelected.toggle()
}
To select only the pressed button out of three buttons and deselect the others when designated button is pressed.
#IBAction func buttonPressed(_ sender: UIButton) {
// Deselect all buttons
button1.isSelected = false
button2.isSelected = false
button3.isSelected = false
// Select the pressed button
sender.isSelected = true
}

UIButton text not staying changing

I am developing a simple iPhone app in Swift
I have a customUITableViewCell. In the cell I have a normal UIButton with text of "Hello World".
In my code for the custom cell, I have the following function:
#IBAction func buttonAction(sender: AnyObject) {
if likeButton.titleLabel?.text == "Hello World" {
likeButton.titleLabel?.text = "Hi!"
}
}
When I tap theUIButton, the text of theUIButton changes to "Hi!" for just a split second! And then it quickly changes back to "Hello World"...is there any way to fix that?
Why is it doing that?
Use UIButtons's setTitle: forState: rather than setting text on titleLabel of button. For Instance-
Initial set up
likeButton.setTitle("Hello World", forState: UIControlState.Normal)
On click set up-
#IBAction func buttonAction(sender: AnyObject) {
if likeButton.titleLabel?.text == "Hello World" {
likeButton.setTitle("Hi!", forState: UIControlState.Normal)
}
}
likeButton.setTitle("my text here", forState: UIControlState.Normal)
Use this code
Do Something like this :
#IBAction func buttonAction(sender: AnyObject) {
likeButton.setTitle("Hi!", forState: UIControlState.Normal)
}
it works here, hope also work there, set your default value-
yourButton.setTitle("Hello", forState: UIControlState.Normal)
when you tapped your button set this
#IBAction func buttonAction(sender: AnyObject) {
if yourButton.titleLabel?.text == "Hello " {
yourButton.setTitle("Max", forState: UIControlState.Normal)
}
}

Swift if statement being re-executed when button title updated

So I have this very simple code, the button is set to the title "Title1" in IB
#IBAction func changeTitle(sender: AnyObject) {
let button = sender as UIButton
if (button.titleLabel == "Title1") {
button.setTitle("Title2", forState: .Normal)
} else {
button.setTitle("Title1", forState: .Normal)
}
}
So what's happening is that the first condition in the if statement gets executed, then the line of code updating the title of the button is hit, where then it goes back to the if statement, then finding it false, and skipping to the else {} now... I cannot figure out why this is happening.
Thanks in advance
This should work:
#IBAction func changeTitle(sender: AnyObject) {
let button = sender as UIButton
if (button.titleLabel?.text == "Title1") {
button.setTitle("Title2", forState: .Normal)
} else {
button.setTitle("Title1", forState: .Normal)
}
}

Resources