Reputation: 31
i have to code an application for school project that we have to get a library of PDF with categories and a detail view of a selected PDF. The problem is that when i select a category the application crash and i get this error "fatal error: unexpectedly found nil while unwrapping an Optional value"
I have noticed that's when i reload data f this view that it crashes.
The line where i get the error is Open.target = self.revealViewController()
Here's my entire file :
//
// TableViewControllertest.swift
// ProjetSwift
//
// Created by geoffrey dalfin on 08/04/2016.
// Copyright © 2016 geoffrey dalfin. All rights reserved.
//
import UIKit
import Alamofire
class TableViewControllertest: UITableViewController {
@IBOutlet weak var titrePDF: UILabel!
@IBOutlet weak var Open: UIBarButtonItem!
@IBOutlet weak var titleView: UINavigationItem!
var TabPDF = [Dictionary<String, AnyObject>]()
var selectedCategorie = String("")
override func viewDidLoad() {
if selectedCategorie == "" {
loadDataPDF()
} else {
LoadPDFCateorie(selectedCategorie)
}
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
Open.target = self.revealViewController() ***<== ERROR COMES FROM HERE***
//Quand on clique dessus, sa on appele le revealViewController
Open.action = Selector("revealToggle:")
// 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()
}
func loadDataPDF(){
Alamofire.request(.GET, "http://perso.montpellier.epsi.fr/~geoffrey.dalfin/ProjetE4/requetes/ListePDF.php").responseJSON{
response in switch response.result{
case.Success:
if let PDF = response.result.value as? [Dictionary<String, AnyObject>]{
self.TabPDF = PDF
self.tableView.reloadData()
}
case.Failure(let error):
print(error)
}
}
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return TabPDF.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cellule = tableView.dequeueReusableCellWithIdentifier("cellule" ,forIndexPath: indexPath) as! TableViewCell
let P = TabPDF[indexPath.row]
cellule.titrePDF?.text = P["Nom"] as? String
return cellule
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "detail" {
var details = segue.destinationViewController as!ViewDetailPDF
var indexPath = NSIndexPath()
indexPath = self.tableView.indexPathForSelectedRow!;
details.test = ((TabPDF[indexPath.row] as! [String: AnyObject]))
}
}
func LoadPDFCateorie(categorie : String){
print(categorie)
Alamofire.request(.GET, "http://perso.montpellier.epsi.fr/~geoffrey.dalfin/ProjetE4/requetes/RequeteCategorie.php?categorie="+categorie).responseJSON{
response in switch response.result{
case.Success:
print(String(response.result.value))
if let PDF = response.result.value as? [Dictionary<String, AnyObject>]{
self.TabPDF = PDF
self.tableView.reloadData()
}
case.Failure(let error):
print(error)
}
}
}
// 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?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
if (segue.identifier == "detail") {
// pass data to next view
var nextScene = segue.destinationViewController as! ViewDetailPDF
if let indexPath = self.tableView.indexPathForSelectedRow! {
// Pass the selected object to the new view controller
let selectedPDF = TabPDF[indexPath.row]
nextScene.DetailPDF = selectedPDF
}
}
}*/
/* override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { // Fonction pour passer des données d'un controller à un autre
let destViewController : ViewDetailPDF = segue.destinationViewController as! ViewDetailPDF
let selectedIndex = self.tableView.indexPathForCell(sender as! UITableViewCell)
destViewController.DetailPDF = TabPDF // On fait passer nos notes
}*/
}
i have a file with the function in it but it has been developed by a friend using a plugin so i hope this is the good part of the file:
@protocol SWRevealViewControllerDelegate<NSObject>
@optional
// The following delegate methods will be called before and after the front view moves to a position
- (void)revealController:(SWRevealViewController *)revealController willMoveToPosition:(FrontViewPosition)position;
- (void)revealController:(SWRevealViewController *)revealController didMoveToPosition:(FrontViewPosition)position;
// This will be called inside the reveal animation, thus you can use it to place your own code that will be animated in sync
- (void)revealController:(SWRevealViewController *)revealController animateToPosition:(FrontViewPosition)position;
Upvotes: 2
Views: 1362
Reputation: 130102
self.revealViewController()
returns the parent controller of type SWRevealViewController
.
If your controller is not in the view hierarchy of a SWRevealViewController
, the method will return nil
.
In viewDidLoad
your controller has no parents and revealViewController()
will always return nil
.
You should call revealViewController()
only when your controller is visible, that means in viewDidAppear:
. Never in viewDidLoad
.
Upvotes: 6