If statements for code generated buttons, Swift

98 views Asked by At

I have read a lot on Staked Overflow and watched youtube videos about generating buttons with code, but I can't figure out how to have an if statement based off the button. This is nested inside of another statement because when one question is answered with the oringal yes or no buttons I have another question is asked.

I have:

var btn = UIButton(frame: CGRectMake(50, 50, 150, 20));
btn.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal);
btn.setTitle("My Button Text!!", forState:UIControlState.Normal);
btn.addTarget(self, action: "buttonTapped:", forControlEvents: UIControlEvents.TouchUpInside);
self.view.addSubview(btn);

func buttonTapped(sender: UIButton!) {
    println("Button Tapped!!!")
}

But rather than printing "Button Tapped!!!" I get a "SIGABART" error. How could I use an if statement instead of a function one? Is there a better way to wright this function statement?

The full code is:

//
//  ViewController.swift
//  Living Vermont
//
//  Created by Matthew Furtsch on 6/8/15.
//  Copyright (c) 2015 Matthew Furtsch. All rights reserved.
//

import UIKit

class ViewController: UIViewController {


    var label1: UILabel!
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.



        label1 = UILabel()
        label1.text = "Is it a plant?"
        label1.font = UIFont.systemFontOfSize(36)
        label1.sizeToFit()
        //Label location quardnets
        label1.center = CGPoint(x: 100, y: 0)
        view.addSubview(label1)
        UIView.animateWithDuration(1.5, delay: 0.0, usingSpringWithDamping: 0.2, initialSpringVelocity: 0.0, options: nil, animations: {
            self.label1.center = CGPoint(x: 100, y: 0 + 40)
        }, completion: nil)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @IBAction func journal(sender: AnyObject)
    {

    }
    @IBAction func noButton(sender: AnyObject)
    {
        label1.text = "no"
        var btn = UIButton(frame: CGRectMake(50, 50, 150, 20));
        btn.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal);
        btn.setTitle("My Button Text!!", forState:UIControlState.Normal);
        btn.addTarget(self, action: "buttonTapped:", forControlEvents: UIControlEvents.TouchUpInside);
        self.view.addSubview(btn);

         func buttonTapped(sender: UIButton!) {
            println("Button Tapped!!!")
        }
    }
    @IBAction func yesButton(sender: AnyObject)
    {

        label1.text = "yes"
         }
}
1

There are 1 answers

0
Duncan C On

My guess is that your buttonTapped function is nested inside another method like viewDidLoad. It needs to be a top-level method of your view controller class. Move your buttonTapped method up so it is inside your class, but not inside any methods inside your class.

You should edit your question and include the entire crash message you are getting, along with the stack trace. That might help us tell what's going wrong.