Ok here is what is going on. I have a table view class called MainTabeViewController. I have a sidebar class called SettingsSidebarViewController that uses SW Reveal to show a menu. The menu is toggled by a bar button item called settings. The menu works fine with the bar button item, and when you press it the menu toggles like it should.
However, once I click a switch, the app crashes and I start getting a EXC_BAD_INSTRUCTION error that reads in the console Fatal error: unexpectedly found nil while unwrapping an optional value. Why is the bar button item suddenly nil after the switch is pressed?
MAINTABLEVIEWCONTROLLER.swift
import UIKit
import SwiftyJSON
class MainTableViewController: UITableViewController, SettingsSidebarViewDelegate {
//HEERE IS THE BAR BUTTON ITEM CALLED SETTINGS <- <- <-
#IBOutlet var settings: UIBarButtonItem!
var NumberofRows = 0
var names = [String]()
var descriptions = [String]()
var categories = [String]()
var types = [String]()
var series = [String]()
var groups = [String]()
func parseJSON(){
let path = NSBundle.mainBundle().URLForResource("documents", withExtension: "json")
let data = NSData(contentsOfURL: path!) as NSData!
let readableJSON = JSON(data: data)
NumberofRows = readableJSON["Documents"].count
for i in 1...NumberofRows {
let doc = "Doc" + "\(i)"
let Name = readableJSON["Documents"][doc]["name"].string as String!
let Description = readableJSON["Documents"][doc]["description"].string as String!
let Category = readableJSON["Documents"][doc]["category"].string as String!
let Type = readableJSON["Documents"][doc]["type"].string as String!
let Series = readableJSON["Documents"][doc]["tags"]["series"].string as String!
let Group = readableJSON["Documents"][doc]["tags"]["group"].string as String!
names.append(Name)
descriptions.append(Description)
categories.append(Category)
types.append(Type)
series.append(Series)
groups.append(Group)
}
}
Here is where the errors start to occur AFTER the switch is pressed (still in same class)
func initSettings(){
//Sets button title to gear, sets button actions (to open menu)
settings.title = NSString(string: "\u{2699}\u{0000FE0E}") as String!
let font = UIFont.systemFontOfSize(25);
settings.setTitleTextAttributes([NSFontAttributeName: font], forState:UIControlState.Normal)
settings.target = self.revealViewController()
settings.action = #selector(SWRevealViewController.rightRevealToggle(_:))
self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
}
func showTags(showTags: Bool) {
tableView.reloadData()
}
func showTimestamp(showTimeStamp: Bool) {
tableView.reloadData()
}
override func viewDidLoad() {
super.viewDidLoad()
parseJSON()
initSettings()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return NumberofRows
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("MainTableCell", forIndexPath: indexPath) as! MainTableViewCell
if names.count != 0{
cell.fileName.text = names[indexPath.row]
cell.fileDescription.text = descriptions[indexPath.row]
cell.fileCategory.text = categories[indexPath.row]
cell.fileType.text = types[indexPath.row]
cell.options.setTitle(NSString(string: ":") as String!, forState: .Normal)
cell.tag1.text = series[indexPath.row]
cell.tag2.text = groups[indexPath.row]
if showTagsVal{
cell.tag1.hidden = false
}
else{
cell.tag1.hidden = true
}
if showTimeStampVal{
cell.tag2.hidden = false
}
else{
cell.tag2.hidden = true
}
}
return cell
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
self.performSegueWithIdentifier("showView", sender: self)
}
// MARK: - Navigation
//In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if(segue.identifier == "showView"){
let detailVC: DetailViewController = segue.destinationViewController as! DetailViewController
let indexPath = self.tableView.indexPathForSelectedRow!
detailVC.text = names[indexPath.row]
self.tableView.deselectRowAtIndexPath(indexPath, animated: true)
}}}
SettingsSidebarViewController.swift
import UIKit
protocol SettingsSidebarViewDelegate{
func showTags(showTags: Bool);
func showTimestamp(showTimeStamp: Bool)
}
var showTagsVal = false
var showTimeStampVal = false
class SettingsSidebarViewController: UIViewController {
var delegate: SettingsSidebarViewDelegate! = nil
#IBOutlet weak var sidebar_title: UILabel!
#IBOutlet var showTagsSwitch: UISwitch!
#IBOutlet var showTimestampSwitch: UISwitch!
#IBAction func switchPressed(sender: AnyObject) {
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let nextViewController = storyBoard.instantiateViewControllerWithIdentifier("main") as! MainTableViewController
self.presentViewController(nextViewController, animated:true, completion:nil)
let vc = MainTableViewController()
self.delegate = vc
if showTagsSwitch.on{
showTagsVal = true
delegate.showTags(showTagsVal)
}
else{
showTagsVal = false
delegate.showTags(showTagsVal)
}
if showTimestampSwitch.on{
showTimeStampVal = true
delegate.showTimestamp(showTimeStampVal)
}
else{
showTimeStampVal = false
delegate.showTimestamp(showTimeStampVal)
}
}
override func viewDidLoad() {
super.viewDidLoad()
sidebar_title.text = "Settings"
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
Help is appreciated! I am sure this is a question concerning transitioning view controllers that is something easy but I have tried too long to figure it out.
Your problem is you are creating a new instance of MainTableViewController and assigning it to delegate. That's why the bar button item is nil, because all the initialization and binding isn't done.
You have to change the delegate and assign the view controller you already got with instantiateViewControllerWithIdentifier:
self.delegate = nextViewController
Related
I am new in programming, and have problem. I am using parse for my array in tableview. When the row is selected i want to segue on another view controller to textView. The tableview works good but i can't get text to textView.
tableVC:
import UIKit
import Parse
class ThirdTableVC: UITableViewController {
#IBOutlet weak var refresherQuotes: UIRefreshControl!
#IBOutlet var quoteTable: UITableView!
var selectedQuote: PFObject?
var quoteItems = [PFObject]()
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func updateQuotesResults(_ sender: UIRefreshControl) {
fetchQuotesData()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
loadQuoteTexts(selectedQuote: selectedQuote)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return quoteItems.count
}
func fetchQuotesData() {
let quotesQuery = PFQuery(className: "TotalTest")
quotesQuery.whereKey("Subcategory", equalTo: selectedQuote ?? nil)
quotesQuery.findObjectsInBackground { (objects, error) in
if let realCategoryObjects = objects {
self.quoteItems = realCategoryObjects
self.tableView.reloadData()
self.refresherQuotes.endRefreshing()
}
}
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let quoteCell = tableView.dequeueReusableCell(withIdentifier: "quoteCell", for: indexPath)
let quoteItem = quoteItems[indexPath.row]
let quoteUserTitle = quoteItem.object(forKey: "TextQuote") as? String
quoteCell.textLabel?.text = quoteUserTitle
return quoteCell
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showQuoteDetail" {
if let indexPath = tableView.indexPathForSelectedRow {
let quoteobject = quoteItems[indexPath.row] as? NSDate
let quoteController = (segue.destination as! UINavigationController).topViewController as! DetailViewController
quoteController.detailItem = quoteobject
quoteController.navigationItem.leftBarButtonItem = splitViewController?.displayModeButtonItem
quoteController.navigationItem.leftItemsSupplementBackButton = true
}
}
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("Row tapped: \(indexPath.row)")
let selectedQuotes: PFObject = quoteItems[indexPath.row]
let FourthVC = self.storyboard?.instantiateViewController(withIdentifier: "FourthViewController") as! FourthViewController
FourthVC.fourthTextView.text = quoteItems[indexPath.row] as? String
self.navigationController?.pushViewController(FourthVC, animated: true)
}
func loadQuoteTexts(selectedQuote: PFObject!) {
let quoteQuery = PFQuery(className: "TotalQuote")
quoteQuery.whereKey("QuoteSubs", equalTo: selectedQuote ?? nil)
quoteQuery.includeKey("QuoteSubs")
quoteQuery.findObjectsInBackground { (result: [PFObject]?, error) in
if let searchQuoteResults = result {
self.quoteItems = searchQuoteResults
self.quoteTable.reloadData()
}
}
}
}
How can I change this?
viewController with textView:
import UIKit
import Parse
class FourthViewController: UIViewController {
var getQuote: PFObject?
#IBOutlet weak var fourthTextView: UITextView!
#IBOutlet weak var fourthLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
fourthLabel.text! = getQuote as! String
fourthTextView.text! = getQuote as! String
}
}
Please help me to passing texts
If you use pushViewController do it like that , in did selectRowAt
let MainStory: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let desVC = MainStory.instantiateViewController(withIdentifier: "FourthViewController") as! FourthViewController
and now pass your text
desVC.getText = "here goes your text u want to pass"
FourthViewController
set up your var
var getText = String()
so you can finally use
self.navigationController?.pushViewController(desVC, animated: true)
so it will pass all parameters you previous add with desVC.getSomething
in FourthViewController you just need to use getText.
The problem is that you are changing from a view to another with pushViewController, by doing that your prepareForSegue won't be executed.
On your didSelectRow you need to call performSegue(withIdentifier:sender:).
You can lookup this question for more information on how to do it.
I'm trying to send values from one view to other and print them in a table as an array. The program work and display the data but the problem is that when I try to add another value to the table when I return to the view that have the table the previous values are no longer there.
In this segment of code I sent the data to the other view
import UIKit
class NewContactoViewController: UIViewController, UITextFieldDelegate {
var contacto: String = ""
var numero: String = ""
#IBOutlet weak var contactoField: UITextField!
#IBOutlet weak var numField: UITextField!
let defaultValues = UserDefaults.standard
#IBAction func addButton(_ sender: UIButton) {
contacto = contactoField.text!
numero = numField.text!
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
var secondController = segue.destination as! ContactosViewController
secondController.contactos = contactoField.text!
secondController.numerosmov = numField.text!
}
override func viewDidLoad() {
super.viewDidLoad()
self.contactoField.delegate = self
self.numField.delegate = self
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
In this segment of code are the tableviews that display the data
import UIKit
class ContactosViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
let defaultValues = UserDefaults.standard
var contactos: String = ""
var numerosmov: String = ""
var tablacontacto = [String] ()
var tablanumero = [String] ()
let cellIdentifier: String = "cell"
let cellIdentifier2: String = "cell2"
#IBOutlet weak var contactoTable: UITableView!
#IBOutlet weak var numTable: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
let backButton = UIBarButtonItem(title: "", style: UIBarButtonItemStyle.plain, target: navigationController, action: nil)
navigationItem.leftBarButtonItem = backButton
// Do any additional setup after loading the view.
datosRecividos(contactos, numerosmov)
contactoTable.delegate = self
numTable.delegate = self
contactoTable.dataSource = self
numTable.dataSource = self
contactoTable!.reloadData()
numTable!.reloadData()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(true)
contactoTable.reloadData()
numTable.reloadData()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func datosRecividos(_ contactosr: String, _ numerosr: String)
{
tablacontacto.append(contactosr)
tablanumero.append(numerosr)
let usercontacto = defaultValues.array(forKey: "contactoTable")
let usernumero = defaultValues.array(forKey: "numeroTable")
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if (tableView.tag == 1)
{
return(tablacontacto.count)
}
else if (tableView.tag == 2)
{
return(tablanumero.count)
}
return 0
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath)
if (tableView.tag == 1)
{
cell.textLabel?.text = tablacontacto[indexPath.row] as! String
}
else if (tableView.tag == 2)
{
cell.textLabel?.text = tablanumero[indexPath.row] as! String
}
return(cell)
}
}
There are several things wrong here, I would suggest reading again about tableView's (especially the "Load Initial Data" section) -
https://developer.apple.com/library/content/referencelibrary/GettingStarted/DevelopiOSAppsSwift/CreateATableView.html
Your tables are getting data from the "tablacontacto" and "tablanumero" arrays.
There is no place in the code you sent to populate these arrays. (Do you see anything when these tables are on screen?)
Plus You are updating these arrays with only in the "func datosRecividos(_ contactosr: String, _ numerosr: String)" -
This method is only called once in the viewDidLoad and it is not called when you segue back to this screen
Plus you have no place you get data to your arrays from your userDefaults and there is no place you save the "new" data from "NewContactoViewController" to your userDefaults.
Make this two variables as static variables
internal static var CONTACTTO: String = ""
internal static var NUMERO: String = ""
then access this variables using,
NewContactoViewController.CONTACTTO
NewContactoViewController.NUMERO
Then there is no need sending this values using
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
var secondController = segue.destination as! ContactosViewController
secondController.contactos = contactoField.text!
secondController.numerosmov = numField.text!
}
Or otherwise you can save these values in shared preferences
You can declare a delegate for data changing in NewContactoViewController
import UIKit
//Create a delegate for data changing
protocol ContactChageDelegate: class {
func contactChanged(newContact: String, newNumber: String)
}
class NewContactoViewController: UIViewController, UITextFieldDelegate {
//declare delegate variable
weak var contactChageDelegate: ContactChageDelegate?
...
#IBAction func addButton(_ sender: UIButton) {
contacto = contactoField.text!
numero = numField.text!
//if need notify data changing. maybe it will not change
//if data change {
self.contactChageDelegate.contactChanged(newContact: contacto, newNumber: numero)
//}
}
and use this delegate in ContactosViewController like this:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
//let self to delegate
if let newContactoViewController = segue.destination as? NewContactoViewController {
newContactoViewController.contactChageDelegate = self
}
}
...
//implement delegate method
extension ContactosViewController: ContactChageDelegate {
internal func contactChanged(newContact: String, newNumber: String) {
//now you have new values
//change your data array
//and reload table
}
}
I'm building a table view for shops in our town, and I'd like to be able to click on the name of the shop to see some more details. You can find some pictures of the main.storyboard and the files here. I also added a video of the problem with the clicking.
The code of the masterViewController is mentioned below:
import UIKit
class MasterTableViewController: UITableViewController {
// MARK: - Properties
var detailViewController: DetailViewController? = nil
var winkels = [Winkel]()
// MARK: - View Setup
override func viewDidLoad() {
super.viewDidLoad()
winkels = [
Winkel(category:"Literature", name:"Standaard"),
Winkel(category:"Literature", name:"Acco"),
Winkel(category:"Clothing", name:"H&M"),
Winkel(category:"Clothing", name:"C&A"),
Winkel(category:"Clothing", name:"Patio"),
Winkel(category:"Restaurants", name:"De 46"),
Winkel(category:"Restaurants", name:"Het hoekske"),
Winkel(category:"Supermarkets", name:"Carrefour"),
Winkel(category:"Supermarkets", name:"Colruyt")
]
winkels.sortInPlace({ $0.name < $1.name })
if let splitViewController = splitViewController {
let controllers = splitViewController.viewControllers
detailViewController = (controllers[controllers.count - 1] as! UINavigationController).topViewController as? DetailViewController
}
self.tableView.reloadData()
}
override func viewWillAppear(animated: Bool) {
clearsSelectionOnViewWillAppear = splitViewController!.collapsed
super.viewWillAppear(animated)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
// MARK: - Table View
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return winkels.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)
let winkel = winkels[indexPath.row]
cell.textLabel!.text = winkel.name
cell.detailTextLabel?.text = winkel.category
return cell
}
// MARK: - Segues
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "showDetail" {
if let indexPath = tableView.indexPathForSelectedRow {
let winkel = winkels[indexPath.row]
let controller = (segue.destinationViewController as! UINavigationController).topViewController as! DetailViewController
controller.detailWinkel = winkel
controller.navigationItem.leftBarButtonItem = splitViewController?.displayModeButtonItem()
controller.navigationItem.leftItemsSupplementBackButton = true
}
}
}
}
The last part shows the segue to the next navigation controller (named showDetail, and is of kind "show Detail (e.g. replace)".
Below is the code of the detailViewController:
import UIKit
class DetailViewController: UIViewController {
#IBOutlet weak var detailDescriptionLabel: UILabel!
#IBOutlet weak var WinkelImageView: UIImageView!
var detailWinkel: Winkel? {
didSet {
configureView()
}
}
func configureView() {
if let detailWinkel = detailWinkel {
if let detailDescriptionLabel = detailDescriptionLabel, WinkelImageView = WinkelImageView {
detailDescriptionLabel.text = detailWinkel.name
WinkelImageView.image = UIImage(named: detailWinkel.name)
title = detailWinkel.category
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
configureView()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
I'm not sure what i'm doing wrong so that the clicking won't work.. Thanks in advance for teaching me how to fix this!
You need to implement the method didSelectRowAtIndexPath for the tableview in the first class. In this, you will perform the segue that actually moves from one ViewController to another.
Im creating an app where different buttons in a ViewController load different menu's into the tableViewController. The buttons are linked by a prepare for segue and the menu's (arrays) are linked by a contentMode. 1: breakfast menu & 2: lunch menu. I had allot of help from someone setting this up but now the table is not loading any data... The cell has 3 labels which display an item, info and price. It changes value within the code when a contentMode is selected. Does anyone see the problem in my code? thanks a lot!
import UIKit
class foodMenuController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let foodMenuController = segue.destinationViewController as! foodTableViewController
if segue.identifier == "showBreakfast" {
foodMenuController.contentMode = 1
} else if segue.identifier == "showLunch" {
foodMenuController.contentMode = 2
}
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
import UIKit
class foodTableViewCell: UITableViewCell {
#IBOutlet weak var foodItem: UILabel!
#IBOutlet weak var foodDescription: UILabel!
#IBOutlet weak var foodPrice: 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
}
}
import UIKit
class foodTableViewController: UITableViewController {
//Content Mode Selection in menu
var contentMode = 0
// THIS SHOULD BE LOADED WHEN CONTENT MODE is "1" --> BREAKFAST
let breakfastItems = ["Bread", "Coffee", "Nada"]
let breakfastInfo = ["Good", "Nice", "Nothing"]
let breakfastPrice = ["$1", "$100", "$12,40"]
// THIS SHOULD BE LOADED WHEN CONTENT MODE IS "2" --> LUNCH
let lunchItems = ["Not bread", "Not Coffee", "Something"]
let lunchInfo = ["Not good", "Not nice", "Yes"]
let lunchPrice = ["$1", "$100", "$12,40"]
var foodItems: [String] = []
var foodInfo: [String] = []
var foodPrice: [String] = []
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(true)
switch (contentMode){
case 1: contentMode = 1
foodItems = breakfastItems
foodInfo = breakfastInfo
foodPrice = breakfastPrice
case 2: contentMode = 2
foodItems = lunchItems
foodInfo = lunchInfo
foodPrice = lunchPrice
default:
break
}
tableView.reloadData()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section:
Int) -> Int {
return foodItems.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cellIdentifier = "Cell"
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! foodTableViewCell
cell.foodItem.text = foodItems[indexPath.row]
cell.foodDescription.text = foodInfo[indexPath.row]
cell.foodPrice.text = foodPrice[indexPath.row]
return cell
}
}
There isn't anything apparently wrong with the snippet you shared. You can check what is returned in the tableView:numberOfRowsInSection: method and see if it is returning a value > 0
Also, this is a given but we've all done it at some point of time - check to make sure the tableview delegate and datasource are set to your viewcontroller.
I have made slight modifications in your project.
1. make the UINavigationController the InitialViewController
2. make the FoodMenuController the root of UINavigationController
Now modify your FoodMenuController
#IBOutlet weak var bakeryButton: UIButton!
#IBOutlet weak var breakfastButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.navigationBarHidden = true //hide navigationBar in first ViewController
self.bakeryButton.addTarget(self, action: "bakeryButtonAction:", forControlEvents: .TouchUpInside)
self.breakfastButton.addTarget(self, action: "breakfastButtonAction:", forControlEvents: .TouchUpInside)
}
func bakeryButtonAction(sender: UIButton) {
performSegueWithIdentifier("showLunch", sender: self)
}
func breakfastButtonAction(sender: UIButton) {
performSegueWithIdentifier("showBreakfast", sender: self)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let foodTableViewController: FoodTableViewController = segue.destinationViewController as! FoodTableViewController
if segue.identifier == "showBreakfast" {
foodTableViewController.contentMode = 1
} else if segue.identifier == "showLunch" {
foodTableViewController.contentMode = 2
}
}
Also you can make UINavigationBar visible in FoodTableViewController
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.navigationBarHidden = false
}
PS: It is always better not to add segue directly to a UIButton. Alternatively you can add it from the yellow button on top of your FoodMenuController and specify the segue to be fired in UIButtonAction using performSegueWithIdentifier
I can no where see you setting the datasource and delegate of the tableView, please cross check these are both setup.
My app keeps crashing when I select the TableViewCell but it does not give me an error message. Hope some on can help. Below is the TableView Controller and the View Controller code. I have added the date into the cordite model and think it has something to do with that.
import UIKit
import CoreData
class DiveLogTableViewController: UITableViewController {
var myDivelog : Array<AnyObject> = []
override func viewDidLoad() {
super.viewDidLoad()
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem()
}
override func viewDidAppear(animated: Bool) {
let appDel:AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let context:NSManagedObjectContext = appDel.managedObjectContext!
let freq = NSFetchRequest(entityName: "Divelog")
myDivelog = context.executeFetchRequest(freq, error: nil)!
tableView.reloadData()
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "update" {
var selectedItem: NSManagedObject = myDivelog[self.tableView.indexPathForSelectedRow()!.row] as! NSManagedObject
let ADLVC: AddDiveLogViewController = segue.destinationViewController as! AddDiveLogViewController
ADLVC.divenumber = selectedItem.valueForKey("divenumber") as! String
ADLVC.ddate = selectedItem.valueForKey("ddate") as! NSDate
ADLVC.divelocation = selectedItem.valueForKey("divelocation") as! String
ADLVC.existingItem = selectedItem
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Potentially incomplete method implementation.
// Return the number of sections.
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete method implementation.
// Return the number of rows in the section.
return myDivelog.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let CellID: NSString = "Cell"
var cell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier(CellID as String) as! UITableViewCell
if let ip = indexPath as NSIndexPath? {
var data: NSManagedObject = myDivelog[ip.row] as! NSManagedObject
var ddate = data.valueForKey("ddate") as! NSDate
var diveloc = data.valueForKey("divelocation") as! String
var diveno = data.valueForKey("divenumber") as! String
cell.textLabel!.text = "#\(diveno)#\(diveloc)"
cell.detailTextLabel!.text = "\(ddate),location: \(diveloc)"
}
// Configure the cell...
return cell
}
// Override to support conditional editing of the table view.
override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
// Return NO if you do not want the specified item to be editable.
return true
}
// Override to support editing the table view.
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
let appDel:AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let context:NSManagedObjectContext = appDel.managedObjectContext!
if editingStyle == UITableViewCellEditingStyle.Delete {
if let tv = tableView as UITableView? {
context.deleteObject(myDivelog[indexPath.row] as! NSManagedObject)
myDivelog.removeAtIndex(indexPath.row)
tv.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Fade)
}
var error: NSError? = nil
if !context.save(&error) {
abort()
}
}
}
}
import UIKit
import CoreData
class AddDiveLogViewController: UIViewController {
#IBOutlet weak var textFieldDiveNumber: UITextField!
#IBOutlet weak var textFieldDiveLocation: UITextField!
#IBOutlet weak var textFieldDDate: UITextField!
var divenumber: String = ""
var divelocation: String = ""
var ddate = NSDate()
var datePickerView: UIDatePicker!
var existingItem: NSManagedObject!
override func viewDidLoad() {
super.viewDidLoad()
if (existingItem != nil) {
textFieldDiveNumber.text = divenumber
textFieldDiveLocation.text = divelocation
textFieldDDate.text = ddate.stringValue
}
// Do any additional setup after loading the view.
datePickerView = UIDatePicker()
datePickerView.datePickerMode = UIDatePickerMode.Date
var toolbar = UIToolbar(frame: CGRectMake(0, 0, datePickerView.frame.width, 44))
let OKButton = UIBarButtonItem(title: "OK", style: .Plain, target: self, action: "OKButtonTapped:")
toolbar.setItems([OKButton], animated: true)
self.textFieldDDate.inputView = datePickerView
self.textFieldDDate.inputAccessoryView = toolbar
}
#IBAction func saveTapped(sender: AnyObject) {
let appDel: AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let contxt: NSManagedObjectContext = appDel.managedObjectContext!
let en = NSEntityDescription.entityForName("Divelog", inManagedObjectContext: contxt)
if (existingItem != nil) {
existingItem.setValue(textFieldDiveNumber.text, forKey: "divenumber")
existingItem.setValue(textFieldDiveLocation.text, forKey: "divelocation")
existingItem.setValue(textFieldDDate.text.dateValue!, forKey: "ddate")
} else {
var newItem = Divelog(entity: en!, insertIntoManagedObjectContext: contxt)
newItem.divenumber = textFieldDiveNumber.text
newItem.divelocation = textFieldDiveLocation.text
newItem.ddate = textFieldDDate.text.dateValue!
}
contxt.save(nil)
self.navigationController?.popToRootViewControllerAnimated(true)
}
#IBAction func cancelTapped(sender: AnyObject) {
self.navigationController?.popToRootViewControllerAnimated(true)
}
func OKButtonTapped(sender: UIBarButtonItem) {
self.textFieldDDate.endEditing(true)
self.textFieldDDate.text = datePickerView.date.stringValue
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}