gpt4 book ai didi

ios - 无法将数据从 UITableviewController 传递到 ViewController - 不断收到 Swift 动态 Cast 类无条件错误

转载 作者:行者123 更新时间:2023-11-29 02:18:14 25 4
gpt4 key购买 nike

我只是想将 TableViewController 中显示的核心数据传递到另一个 ViewController - 我的prepareForSegueCode 似乎有问题。尝试传递数据并将其加载到 ViewController 的 viewDidLoad 中。非常感谢任何帮助 - 已经为此工作了几个小时但没有运气。

TableViewController代码:

import UIKit
import CoreData

class MyEventsTableViewController: UITableViewController, NSFetchedResultsControllerDelegate {

var myEvents:[myEventsData] = []

var fetchResultController: NSFetchedResultsController!

func controllerWillChangeContent(controller: NSFetchedResultsController!) {
tableView.beginUpdates()
}

func controller(controller: NSFetchedResultsController!, didChangeObject anObject: AnyObject!,atIndexPath indexPath: NSIndexPath!, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath!){

switch type {
case .Insert:
tableView.insertRowsAtIndexPaths([newIndexPath], withRowAnimation: .Fade)
case .Delete:
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
case .Update:
tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)

default:
tableView.reloadData()
}

myEvents = controller.fetchedObjects as [myEventsData]

}

func controllerDidChangeContent(controller: NSFetchedResultsController) {
tableView.endUpdates()
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

let cellIdentifier = "myEventsCell"
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as MyEventsTableViewCell

let eventData = myEvents [indexPath.row]
cell.eventNameLabel.text = "Name: " + eventData.eventName
cell.startDateLabel.text = "Start: " + eventData.startDate
cell.endDateLabel.text = "End: " + eventData.endDate

cell.eventData = eventData

cell.backgroundColor = UIColor.clearColor()



return cell
}

override func prepareForSegue (segue: UIStoryboardSegue, sender: AnyObject!) {
if segue.identifier == "showEventDetails" {
if let indexPath = self.tableView.indexPathForSelectedRow() {

let destinationController = segue.destinationViewController as MyEventDetailsViewController

destinationController.eventData! = myEvents[indexPath.row]
}
}
}

DestinationViewController 代码:

import UIKit

class MyEventDetailsViewController: UIViewController, UITableViewDelegate {


var eventData:myEventsData!


@IBOutlet weak var eventName: UILabel!
@IBOutlet weak var startDate: UILabel!
@IBOutlet weak var endDate: UILabel!
@IBOutlet weak var details: UILabel!
@IBOutlet weak var postComment: UIButton!
@IBOutlet weak var checkIn: UIButton!
@IBOutlet weak var addPhoto: UIButton!
@IBOutlet weak var myWineJournal: UIButton!


override func viewDidLoad() {
super.viewDidLoad()

// Do any additional setup after loading the view.

self.eventName.text = eventData.eventName
self.startDate.text = eventData.startDate
self.endDate.text = eventData.endDate
self.details.text = eventData.details

}

最佳答案

我在这一行看到一个错误:

destinationController.eventData! = myEvents[indexPath.row]

因为您正在使用强制解包运算符进行赋值 - 这不是必需的,因为它应该只是:

destinationController.eventData = myEvents[indexPath.row]

由于 eventData 属性似乎没有被初始化,我希望有一个不同的异常:

fatal error: unexpectedly found nil while unwrapping an Optional value


附录

强制转换异常可能是这样引起的:

let destinationController = segue.destinationViewController as MyEventDetailsViewController

您确定已在 IB 中为该 View Controller 设置自定义类吗?

更新

如评论中所述,事实证明问题在于源 View Controller 和目标 View Controller 之间存在导航 Controller 。该问题已按照 this answer 中的说明得到解决,包括从导航 Controller 中检索顶 View Controller 。

关于ios - 无法将数据从 UITableviewController 传递到 ViewController - 不断收到 Swift 动态 Cast 类无条件错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28486819/

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