0

My "AppDelegate.swift" file will not connect to my IBAction functions! Please help!!!!

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
 var window: UIWindow?

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { return true }

    func applicationWillResignActive(application: UIApplication) { }

    func applicationDidEnterBackground(application: UIApplication) { }

    func applicationWillEnterForeground(application: UIApplication) { }

    func applicationDidBecomeActive(application: UIApplication) { }

    func applicationWillTerminate(application: UIApplication) { }
}

// Below this line I included the "ViewController.swift" file :

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var Question: UILabel!
    @IBOutlet weak var Answer1: UIButton!
    @IBOutlet weak var Answer2: UIButton!
    var CorrectAnswer = String()


override func viewDidLoad() {
    super.viewDidLoad()
    RandomQuestions() }


override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning() }


func RandomQuestions() {
    var RandomNumber = arc4random() % 2
    RandomNumber += 1

    switch(RandomNumber) {
    case 1 :
        Question.text = "What are two languages used to develop Mac iOS apps?"
        Answer1.setTitle("HTML & JavaScript", forState: UIControlState.Normal)
        Answer2.setTitle("Objective-C & Swift", forState: UIControlState.Normal)
        CorrectAnswer = "2"
        break
    case 2 :
        Question.text = "Which Mac iOS language does not need semicolons?"
        Answer1.setTitle("Swift", forState: UIControlState.Normal)
        Answer2.setTitle("Objective-C", forState: UIControlState.Normal)
        CorrectAnswer = "1"
        break
    default :
        break } }


    @IBAction func Answer1(sender: AnyObject) {
        if (CorrectAnswer == "1") { Question.text = "You are Right!" }
        else if (CorrectAnswer == "2") { Question.text = "You are Right!" }
    }

    @IBAction func Answer2(sender: AnyObject) {
        if (CorrectAnswer == "1") { Question.text = "You are Right!" }
        else if (CorrectAnswer == "2") { Question.text = "You are Right!" }
    }
} 
itsji10dra
  • 4,603
  • 3
  • 39
  • 59
Cody Taylor
  • 31
  • 1
  • 8
  • Can you check the Connections Inspector for the Answer 1 button and see if there are too many connections? Delete connections by clicking the cross, I might delete all connections you see and then ctrl drag from the button back to the code again. – ThrowingSpoon Apr 16 '15 at 15:01
  • It would be real nice if you could include the error which you are getting – Khurram Shehzad Apr 16 '15 at 15:04
  • 2015-04-16 10:04:40.077 Swift_QuizGame[4312:41007] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key Answer1_Action.' – Cody Taylor Apr 16 '15 at 15:05
  • Thank you for trying to help, Khurram Shehzad! – Cody Taylor Apr 16 '15 at 15:14
  • Why do you have an Outlet and an Action for the same button with the same name? The error states that there is no action named "Answer1_Action" in your ViewController. Try to use Connections inspector to see if you have made a mistake on the action's name.See: http://stackoverflow.com/questions/11118135/how-do-i-solve-a-nsunknownkeyexception-setvalueforundefinedkey-not – cleuton Apr 16 '15 at 17:30

0 Answers0