self.revealViewController()错误返回nil

时间:2016-06-05 17:05:03

标签: swift swrevealviewcontroller

我必须为学校项目编写一个应用程序代码,我们必须获得一个PDF类库和一个选定PDF的详细视图。问题是,当我选择一个类别时,应用程序崩溃,我收到此错误“致命错误:在展开Optional值时意外发现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; 

1 个答案:

答案 0 :(得分:5)

self.revealViewController()返回SWRevealViewController类型的父控制器。

如果您的控制器不在SWRevealViewController的视图层次结构中,则该方法将返回nil

viewDidLoad中,您的控制器没有父母,revealViewController()将始终返回nil

只有当您的控制器可见时才会调用revealViewController(),这意味着viewDidAppear:。从不在viewDidLoad