1

I am trying to load multiple food menu arrays into one TableViewController. The main menu displays 2 buttons, 1: breakfast menu 2: bakery menu. Every time I press on of them the simulator crashes and shows the SIGABRT error. After a sleepless night of trying it myself I am reaching out to you to have a look at it.... I checked all connections and buttons with segues and it all checks out..

My cell has 3 labels and an image with each their own array. I divided the buttons to select in different content modes. Content mode 1: breakfast & Content mode 2: bakery..

Sorry guys just really want to learn how to do this!

Button Menu Controller:

import UIKit

class foodMenuController: UIViewController {

@IBOutlet weak var mainMenuButton: UIButton!

var contentMode = 0

//Breakfast mode
@IBAction func breakfastButton(sender: AnyObject) {
    contentMode == 1

}

//Bakery mode
@IBAction func bakeryButton(sender: AnyObject) {
    contentMode = 2
}


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


    //Hide navigation bar
        self.navigationController?.navigationBarHidden = true


     func viewWillAppear(animated: Bool) {
        self.navigationController?.navigationBarHidden = true
    }

}

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

    }

}

TABLEVIEWCONTROLLER

import UIKit

class breakfastFoodControllerTableViewController: UITableViewController {

//Return to Food Menu
@IBOutlet weak var returnButton: UIBarButtonItem!


// BREAKFAST MENU
let breakfastMenu = ["SAVORY CROISSANT", "HEARTY CROISSANT", "LE PETIT DÉJEUNER", "BREAKFAST in AMERICA", "SCRAMBLED EGGS", "FRESH FRUIT BOWL", "OMELETTE", "OATMEAL CUP", "PARIS SANDWICH", "EGGS BENEDICT", "CHEESE BAGEL", "EGGS BALTIQUE", "TOASTED BAGEL", "TOAST"]

// Breakfast menu images
let breakfastMenuImages = ["SAVORY CROISSANT.bmp", "HEARTY CROISSANT.bmp", "LE PETIT DÉJEUNER.bmp", "BREAKFAST in AMERICA.bmp", "SCRAMBLED EGGS.bmp", "FRESH FRUIT BOWL.bmp", "OMELETTE.bmp", "OATMEAL CUP.bmp", "PARIS SANDWICH.bmp", "EGGS BENEDICT.bmp", "CHEESE BAGEL", "EGGS BALTIQUE.bmp", "TOASTED BAGEL.bmp", "TOAST.bmp"]

// Meals definition
let breakfastMealDefined = ["Stuffed with eggs, ham and swiss cheese", "Stuffed with eggs, bacon and swiss cheese", "French baguette with butter, jam", "Two eggs, bacon, sausage, roasted potatoes","Scrambled eggs & smoked salmon","Seasonal fruits with a honey yogurt dressing.","Three fluffy eggs. Add 70 cents per ingredient choice:","Warm oatmeal with milk & extra's","French baguette with ham, cheese & butter","Two poached eggs on an English muffin","Toasted bagel with cream cheese and smoked salmon","Two poached eggs on an English muffin","Toasted bagel with cream cheese & jam","Choose from white, wheat or rye with jelly"]

// Meal price
var breakfastPrice = ["$8,25","$8,25","$7,25","$9,95","$9,25","$5,95","$7,95","$3,95","$9,25","$9,50","$10,25","$11,95","$3,75","$2,75"]

//BAKERY MENU
let bakeryMenu = ["SAVORY CROISSANT", "HEARTY CROISSANT", "LE PETIT DÉJEUNER"]

//Bakery menu images
let bakeryMenuImages = ["SAVORY CROISSANT.bmp", "HEARTY CROISSANT.bmp", "LE PETIT DÉJEUNER.bmp"]

// Bakery definition
let bakeryMealDefined = ["Stuffed with eggs, ham and swiss cheese", "Stuffed with eggs, bacon and swiss cheese", "French baguette with butter, jam"]

// Meal price
var bakeryPrice = ["$8,25","$8,25","$7,25"]


// -->   VIEW DID LOAD <-- //
override func viewDidLoad() {
    super.viewDidLoad()


}


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


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

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    if foodMenuController().contentMode == 1 {
        return breakfastMenu.count
    }
    if foodMenuController().contentMode == 2 {
        return bakeryMenu.count
    } else{

    return foodMenuController().contentMode; 1
}
}

// CONTENT MODE 1

   override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCellWithIdentifier("Cell")! as! firstTableViewSettings

    if foodMenuController().contentMode == 1 {

        // Cell Configuration (image, text labels, round images)

        cell.menuImageOption.image = UIImage (named: breakfastMenu[indexPath.row])
        cell.mealPrice.text = breakfastPrice[indexPath.row]
        cell.mealDescription.text = breakfastMealDefined[indexPath.row]
        cell.menuChoicelabel.text = breakfastMenu[indexPath.row]
        cell.menuImageOption.layer.cornerRadius = 30.0
        cell.menuImageOption.clipsToBounds = true

    }


//CONTENT MODE 2

       if foodMenuController().contentMode == 2 {

            // Cell Configuration (image, text labels, round images)

            cell.menuImageOption.image = UIImage (named: bakeryMenu[indexPath.row])
            cell.mealPrice.text = bakeryPrice[indexPath.row]
            cell.mealDescription.text = bakeryMealDefined[indexPath.row]
            cell.menuChoicelabel.text = bakeryMenu[indexPath.row]
            cell.menuImageOption.layer.cornerRadius = 30.0
            cell.menuImageOption.clipsToBounds = true
        }

       return cell

        }
    }

TABLEVIEWCELL

import UIKit

class firstTableViewSettings: UITableViewCell {

// Outlets for prototype cell configuration


@IBOutlet weak var menuImageOption: UIImageView!
@IBOutlet weak var menuChoicelabel: UILabel!
@IBOutlet weak var mealDescription: UILabel!
@IBOutlet weak var mealPrice: UILabel!


override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
}

override func setSelected(selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

    // Configure the view for the selected state
}

}

Kevin
  • 39
  • 6
  • contentMode == 1 ? one too many '=', I think – Russell Mar 16 '16 at 12:55
  • also - you may want to have a look at defining a class or struct to hold your menu data, and then having a single array to hold that, rather than maintaining several arrays. – Russell Mar 16 '16 at 12:57
  • and then - your breakfast and bakery data structures are identical - if you used a 2-D array (breakfast = index 0, bakery = index 1) then you only need one set of code in the tableview methods to handle everything – Russell Mar 16 '16 at 12:58
  • The main problem is the use of `...foodMenuController()...` in your tableView controller. This creates a new instance of `foodMenuController` - it does not access the existing instance. The solution depends on how the two view controllers are related. Edit your post to explain how they are related - which is displayed first, how and when is the other displayed? – pbasdf Mar 16 '16 at 20:50
  • @pbasdf - one viewcontroller that is the foodMenuController which has 2 buttons displayed. breakfast & bakery, in the foodMenuController I defined 2 different contentModes for this 1 & 2. Both buttons segue to show the tableViewController which should load either the breakfast arrays, or the bakery arrays. However, when I click on either buttons the simulator crashes and I can figure out why? Hope this helps explaining it a bit? Does anyone have a file like this? loading different arrays in a table when a button is clicked in a viewcontroller? just to look at as an example to email me? – Kevin Mar 17 '16 at 07:16
  • @ Kevin Start by reading [this answer](http://stackoverflow.com/a/9736559/3985749) about passing data between view controllers. The code is Objective-C, but you should be able to see what is going on, and the principles are the same for Swift. In your case, you need to pass the data forward from `FoodMenuController` to `BreakfastFoodControllerTableViewController`. This is best done using `prepareForSegue`. I'll try to post a full answer later. – pbasdf Mar 17 '16 at 09:30
  • @pbasdf thanks i really appreciate it. Once i know the technique on how to load different arrays in a tableviewcontroller when a button is clicked in the first viewcontroller I can continue building my app. I hope you can post a answer for me! thank you. – Kevin Mar 17 '16 at 10:35

1 Answers1

0

In general, it is easier to pass data forward, to the next view controller, than it is to get the data from a previous view controller. So, move contentMode to the BreakfastFoodControllerTableViewController (which makes sense; it determines the "mode" of the TVC).

var contentMode = 0

Then amend every reference of foodMenuController().contentMode to just contentMode.

Next, amend the FoodMenuController to set the correct value immediately before the segue to the BreakfastFoodControllerTableViewController. The place to do this is in the FooodMenuController's prepareForSegue method:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    let breakfastVC = segue.destinationViewController as! BreakfastFoodControllerTableViewController
    if segue.identifier == "showBreakfast" {
        breakfastVC.contentMode = 1
    } else if segue.identifier == "showBakery" {
        breakfastVC.contentMode = 2
    }
}

You will need to ensure that each segue has the correct identifier in your storyboard:

enter image description here

The IBAction methods breakfastButton and bakeryButton are no longer required.

pbasdf
  • 21,386
  • 4
  • 43
  • 75
  • I tried your method...however, the contentMode in the TV controller is not changing its value to 1 or 2 when the button is pressed. Am i missing something? – Kevin Mar 30 '16 at 12:31
  • Difficult to say - can you edit your post to show your current code? – pbasdf Mar 31 '16 at 22:36
  • @Kevin Any luck with this? – pbasdf Apr 02 '16 at 08:12
  • Sorry for not replying back to this. I used your method but had to get some extra help from other people. It worked out in the end though! so thank you very much :) – Kevin Apr 03 '16 at 11:48
  • @Kevin No problem; glad you got it sorted. – pbasdf Apr 03 '16 at 11:49