user3558131
user3558131

Reputation: 113

Using cross class variables in swift

I am trying to populate a detail view controller with information based on which table cell is selected. I tried to do what I thought might work, but I am getting the error "use of unresolved identifier 'detail' on the lines that say if (detail) in the detail view controller class.

    @IBOutlet var detailText: UILabel!



override func viewDidLoad() {
    super.viewDidLoad()

    if (detail = "conferenceapp") {
    self.detailText.text = "lol"
    }

    if (detail = "spaceshooter") {
        self.detailText.text = "spaceshooter"
    }

Booth struct

struct Booth {
let category : String
let name : String
let detail : String

}

Table view controller class code snippet

    var booths = [Booth]()
var filteredBooths = [Booth]()

override func viewDidLoad() {
    super.viewDidLoad()

    //fill array with data
    self.booths = [Booth(category: "Tech", name: "Conference App", detail: "conferenceapp"),
        Booth(category: "Tech", name: "Space Shooter", detail: "spaceshooter"),
        Booth(category: "Tech", name: "RollABall", detail: "rollaball"),
        Booth(category: "Animation", name: "Sugar Hill City Model", detail: "sugar"),
        Booth(category: "Animation", name: "3D Sculpting 101", detail: "3d"),
        Booth(category: "Animation", name: "HowTo - Texture", detail: "howto"),
        Booth(category: "Science", name: "AP Biology for Dummies", detail: "apbio"),
        Booth(category: "Science", name: "Cells", detail: "cells"),
        Booth(category: "Science", name: "Space", detail: "space")]

please help, I need to fix the error

Upvotes: 0

Views: 130

Answers (2)

matt
matt

Reputation: 535586

The problem is that you are saying if (detail = "conference app") but there is nothing defined called "detail". Where is any "detail" variable supposed to come from? Are you trying to refer to the detail of a Booth? Then you need a Booth and you need to specify its detail. But you have no Booth and in any case just saying "detail" as a bare word would not refer to its detail.

Also I wonder whether you mean if (detail == "conference app"). You can't use a single equal-sign in Swift the way you are doing.

Upvotes: 1

Steve Rosenberg
Steve Rosenberg

Reputation: 19524

A common way to pass data to the detail view is via the prepareForSegue function. Example:

import UIKit

class MasterViewController: UITableViewController {

    let items = ["Bob","Joe"]

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return items.count
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("Item", forIndexPath: indexPath) as UITableViewCell

        cell.textLabel.text = items[indexPath.row]

        return cell
    }

//Passing details to detail VC
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

            let indexPath = self.tableView.indexPathForSelectedRow()
            let theDestination = (segue.destinationViewController as DetailViewController)
            theDestination.itemName = items[indexPath!.row]
    }

}

import UIKit

class DetailViewController: UIViewController {

    @IBOutlet  weak var nameLabel: UILabel!

    var itemName = ""

    override func viewDidLoad() {
        super.viewDidLoad()

        nameLabel.text = itemName
    }

}

Upvotes: 1

Related Questions