我必须为学校项目编写一个应用程序,我们必须获得一个包含类别和选定pdf的详细视图的pdf库。问题是,当我选择一个类别时,应用程序崩溃,并出现以下错误“致命错误:在展开可选值时意外发现nil”
我注意到当我重新加载这个视图的数据时,它崩溃了。
得到错误的行是Open.target = self.revealViewController()
这是我的全部档案:
//
// 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
}*/
}
我有一个包含函数的文件,但是它是由一个朋友使用插件开发的,所以我希望这是文件的好部分:
@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;
最佳答案
self.revealViewController()
返回SWRevealViewController
类型的父控制器。
如果您的控制器不在SWRevealViewController
的视图层次结构中,则该方法将返回nil
。
在viewDidLoad
中,您的控制器没有父级,revealViewController()
将始终返回nil
。
您应该只在控制器可见时调用revealViewController()
,这意味着在viewDidAppear:
中。从不在viewDidLoad
中。
关于swift - self.revealViewController()错误返回nil,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37644556/