1

I am just starting out iOS development, and I'm curious to see what the proper implementation of the view and view controllers are.

For example, I made a simple TicTacToe game with UIImageView's and UITapGestureRecognizer's. I used an outlet collection array, and then added gesture recognizers to each. I understand for something this simple following MVC is not crucial, but I want to learn it correctly. The outlets should technically be in the view, and I am adding UIGestureRecognizers to each in the view with the target object being my ViewController and the action being a function in my ViewController. However, when I do this my program crashes.

This version just adds an x to the UIImage touched:

import UIKit

class TTTView: UIView {
    @IBOutlet weak var display: UILabel! {
        didSet {
            // add gesture recognizers to all spots on board
            for index in 0..<board.count {
                let gestureRecognizer = UITapGestureRecognizer(target: "ViewController", action: "spotPressed:")
                board[index].addGestureRecognizer(gestureRecognizer)
            }
        }
    }
    @IBOutlet var board: [UIImageView]!
}

and my ViewController:

import UIKit

class ViewController: UIViewController {
    var x = UIImage(named: "x")
    // executed when spot is tapped
    func spotPressed(recognizer: UITapGestureRecognizer) {
        let fieldTapped = recognizer.view as UIImageView
        fieldTapped.image = x // implement with protocol

    }
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

    }
}

This code crashes with the error:

2015-04-02 11:17:41.761 TTTv4[11273:356731] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<TTTv4.ViewController 0x7fc7d3f0e210> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key board.

Can anyone help me understand what is going on here?

This is not the same at all as the linked answer. This is in Swift and the error comes from a completely different function that happens to be trying to access a different view controller.

nobody
  • 19,814
  • 17
  • 56
  • 77
kjoh
  • 19
  • 3
  • This does not have anything to do with what language your code is in; you have your class set incorrectly in IB. The answer at the other question correctly solves your problem. – jscs Apr 02 '15 at 20:00

0 Answers0