Segue 以查看控制器崩溃
Posted
技术标签:
【中文标题】Segue 以查看控制器崩溃【英文标题】:Segue to view controller crashing 【发布时间】:2015-09-23 11:52:10 【问题描述】:谁能告诉我我在这里做错了什么?我正在设计一个小应用程序,它正在从我的核心数据中添加、编辑和删除用户。但是,当我选择一行时,我的应用程序崩溃了,这应该将我带到下一个 viewController。 错误是:'致命错误:在展开可选值时意外发现 nil' 在此转换中为 segue 功能做准备失败了:让 contact:Contact = fetchedResultController.objectAtIndexPath(indexPath!) as!联系方式
所以当我点击一行时,它必须转到下一个视图控制器。
这是我的代码:
// 类 ContactsTableViewController
import UIKit
import CoreData
class ContactsTableViewController: UITableViewController, NSFetchedResultsControllerDelegate
// MARK: - Properties
let context = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext
var fetchedResultController: NSFetchedResultsController = NSFetchedResultsController()
// MARK: - Actions
@IBAction func toggleMe(sender: AnyObject)
UberSideBar.toggleWindow()
override func viewDidLoad()
super.viewDidLoad()
fetchedResultController = getFtechedResultController()
fetchedResultController.delegate = self
fetchedResultController.performFetch(nil)
override func didReceiveMemoryWarning()
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int
let numberOfSection = fetchedResultController.sections?.count
return numberOfSection!
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
let numberOfinASection = fetchedResultController.sections?[section].numberOfObjects
return numberOfinASection!
// MARK: - Custom functions
func getFtechedResultController() -> NSFetchedResultsController
fetchedResultController = NSFetchedResultsController(fetchRequest: taskFetchRequest(), managedObjectContext: context!, sectionNameKeyPath: nil, cacheName: nil)
return fetchedResultController
func taskFetchRequest() -> NSFetchRequest
let fetchRequest = NSFetchRequest(entityName: "Contact")
let sortDescriptor = NSSortDescriptor(key: "name", ascending: true)
fetchRequest.sortDescriptors = [sortDescriptor]
return fetchRequest
func controllerDidChangeContent(controller: NSFetchedResultsController)
// Refresh tableview to fetch all data
tableView.reloadData()
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
let cell = tableView.dequeueReusableCellWithIdentifier("ContactCell", forIndexPath: indexPath) as! ContactcellTableViewCell
let contact = fetchedResultController.objectAtIndexPath(indexPath) as! Contact
println(contact.name)
cell.nameLabel?.text = contact.name
return cell
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath)
let managedObject: NSManagedObject = fetchedResultController.objectAtIndexPath(indexPath) as! NSManagedObject
context?.deleteObject(managedObject)
context?.save(nil)
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
performSegueWithIdentifier("editContact", sender: self)
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
if segue.identifier == "editContact"
let indexPath = tableView.indexPathForSelectedRow()
let contactController: NewCategoryViewController = segue.destinationViewController as! NewCategoryViewController
let contact:Contact = fetchedResultController.objectAtIndexPath(indexPath!) as! Contact
contactController.contact = contact
和
import UIKit
import CoreData
class NewCategoryViewController: UIViewController
// MARK: - Properties
var contact: Contact? = nil
// initialize the core data context:
let context = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext
// MARK: - Outlets
@IBOutlet weak var imageHolder: UIImageView!
@IBOutlet weak var nameField: UITextField!
@IBOutlet weak var emailField: UITextField!
@IBOutlet weak var phoneField: UITextField!
@IBOutlet weak var categoryField: UITextField!
// MARK: - Actions
@IBAction func savebtn(sender: AnyObject)
let entity = NSEntityDescription.entityForName("Contact", inManagedObjectContext: context!)
let newContact = Contact(entity: entity!, insertIntoManagedObjectContext: context)
newContact.name = nameField.text
newContact.email = emailField.text
newContact.phone = phoneField.text
//newContact.photo = UIImageJPEGRepresentation(imageHolder.image, 1)
var error: NSError?
context?.save(&error)
if let errorSaving = error
var alert = UIAlertController(title: "Alert", message: "Couldn't save contact !!!", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Click", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
else
nameField.text = ""
emailField.text = ""
phoneField.text = ""
var alert = UIAlertController(title: "Alert", message: "Contact added", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
override func viewDidLoad()
super.viewDidLoad()
if contact != nil
nameField.text = contact?.name
emailField.text = contact?.email
phoneField.text = contact?.phone
override func didReceiveMemoryWarning()
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
任何帮助将不胜感激
【问题讨论】:
显然sender
不是cell对象,它是tableview控制器对象
【参考方案1】:
您不能将 prepareForSegue 中的发件人转换为 ContactcellTableViewCell,因为它是 ContactsTableViewController 类型。
不要做你正在做的事情,为了获得indexPath,只需做
let indexPath = tableView.indexPathForSelectedRow()
并删除这一行:
let cell = sender as! ContactcellTableViewCell
【讨论】:
它崩溃了吗?如果它确实有什么例外?它在哪里? 我试过这个: override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) if segue.identifier == "editContact" let indexPath = tableView.indexPathForSelectedRow()?.row let contactController: NewCategoryViewController = segue.destinationViewController 为! NewCategoryViewController 让contact:Contact = fetchedResultController.objectAtIndexPath(indexPath!) as!联系 contactController.contact = contact 但我收到此错误 NSNumber is not a subtype of NSIndexPath 在这里崩溃:让contact:Contact = fetchedResultController.objectAtIndexPath(indexPath!) as!联系方式 你不能做 indexPath = tableView.indexPathForSelectedRow()?.row. 如果我这样做 let indexPath = tableView.indexPathForSelectedRow(),那么这里就崩溃了 let contact:Contact = fetchedResultController.objectAtIndexPath(indexPath!) as!接触到以下错误:致命错误:unwrapping an Optional value 时意外发现 nil以上是关于Segue 以查看控制器崩溃的主要内容,如果未能解决你的问题,请参考以下文章