gpt4 book ai didi

swift - self.revealViewController() 错误返回 nil

转载 作者:搜寻专家 更新时间:2023-11-01 06:05:53 26 4
gpt4 key购买 nike

我必须为学校项目编写一个应用程序,我们必须获得一个包含类别的 PDF 库和所选 PDF 的详细 View 。问题是,当我选择一个类别时,应用程序崩溃并且出现此错误“ fatal error :在展开可选值时意外发现 nil”我注意到当我重新加载此 View 的数据时它崩溃了。我收到错误的行是 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
}*/




}

我有一个包含该功能的文件,但它是由一位 friend 使用插件开发的,所以我希望这是该文件的重要部分:

   @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 类型的父 Controller 。

如果您的 Controller 不在 SWRevealViewController 的 View 层次结构中,该方法将返回 nil

viewDidLoad 中,您的 Controller 没有父级,revealViewController() 将始终返回 nil

您应该仅在您的 Controller 可见时调用 revealViewController(),这意味着在 viewDidAppear: 中。从不在 viewDidLoad 中。

关于swift - self.revealViewController() 错误返回 nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37644556/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com