我有一个 UITableView VC,其中的单元格链接到 prepare for segue 函数,这很有效。它移动到正确的 VC。 我在链接到调用 performForSegue 的方法的导航项上添加了 rightBarButtonItem,并为其提供了正确的标识符。在我单击右侧按钮之前,一切都运行良好。当我这样做时,我得到:“首先抛出调用堆栈。以 NSException 类型的未捕获异常终止”有人可以帮助我并指出正确的方向吗?下面的代码和打印 list 。谢谢。
class AssignNumberOfRoomsForFloorsVC: UITableViewController {
//MARK: - Properties
private var managedObjectContext: NSManagedObjectContext!
private var storedFloors = [Floors]()
//MARK: - Actions
override func viewDidLoad() {
super.viewDidLoad()
managedObjectContext = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
loadFloorData()
self.tableView.reloadData()
self.navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.done, target: self, action: Selector(("navigateToNextViewController")))
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
private func loadFloorData() {
let request: NSFetchRequest<Floors> = Floors.fetchRequest()
request.returnsObjectsAsFaults = false
do {
storedFloors = try managedObjectContext.fetch(request)
}
catch {
print("could not load data from core \(error.localizedDescription)")
}
}
private func navigateToNextViewController(){
self.performSegue(withIdentifier: "room and alarm type", sender: self)
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return storedFloors.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "floor cell", for: indexPath) as! FloorCell
let floorItem = storedFloors[indexPath.row]
cell.floorNumberTxt.text = String(floorItem.floorNumber)
return cell
}
// MARK: - Navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
var selectedRow = self.tableView.indexPathForSelectedRow
let floorItem = storedFloors[(selectedRow?.row)!]
let destinationController = segue.destination
if let assignRoomsVC = destinationController as? DeclareRoomsVC {
if let identifier = segue.identifier {
switch identifier {
case "assign number of rooms":
let _ = assignRoomsVC.view
assignRoomsVC.floorNumberTxt.text = String(floorItem.floorNumber)
assignRoomsVC.selectedFloor = floorItem.floorNumber
default: break
}
}
}
}
这是控制台中的消息:
2017-03-26 21:50:28.041 PDCiPad[10433:292572] -[PDCiPad.AssignNumberOfRoomsForFloorsVC navigateToNextViewController]: unrecognized selector sent to instance 0x7ff5b7c326c0
2017-03-26 21:50:28.219 PDCiPad[10433:292572] * 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[PDCiPad.AssignNumberOfRoomsForFloorsVC navigateToNextViewController]:无法识别的选择器已发送到实例 0x7ff5b07c326c ' * 首先抛出调用栈: ( 0 CoreFoundation 0x000000010db79d4b 异常预处理 + 171 1 libobjc.A.dylib 0x000000010d1ba21e objc_exception_throw + 48 2 CoreFoundation 0x000000010dbe9f04 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132 3 CoreFoundation 0x000000010daff005 ___forwarding_ + 1013 4 CoreFoundation 0x000000010dafeb88 _CF_forwarding_prep_0 + 120 5 UIKit 0x000000010e6b28bc -[UIApplication sendAction:to:from:forEvent:] + 83 6 UIKit 0x000000010eaf44a1 -[UIBarButtonItem(UIInternal) _sendAction:withEvent:] + 149 7 UIKit 0x000000010e6b28bc -[UIApplication sendAction:to:from:forEvent:] + 83 8 UIKit 0x000000010e838c38 -[UIControl sendAction:to:forEvent:] + 67 9 UIKit 0x000000010e838f51 -[UIControl _sendActionsForEvents:withEvent:] + 444 10 UIKit 0x000000010e8390db -[UIControl _sendActionsForEvents:withEvent:] + 838 11 UIKit 0x000000010e837e4d -[UIControl touchesEnded:withEvent:] + 668 12 UIKit 0x000000010e720545 -[UIWindow _sendTouchesForEvent:] + 2747 13 UIKit 0x000000010e721c33 -[UIWindow 发送事件:] + 4011 14 UIKit 0x000000010e6ce9ab -[UIApplication sendEvent:] + 371 15 UIKit 0x000000010eebb72d <强>dispatchPreprocessedEventFromEventQueue + 3248 16 UIKit 0x000000010eeb4463 __handleEventQueue + 4879 17 CoreFoundation 0x000000010db1e761 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION + 17 18 核心基础 0x000000010db0398c __CFRunLoopDoSources0 + 556 19 核心基础 0x000000010db02e76 __CFRunLoopRun + 918 20 核心基础 0x000000010db02884 CFRunLoopRunSpecific + 420 21 图形服务 0x0000000113908a6f GSEventRunModal + 161 22 UIKit 0x000000010e6b0c68 UIApplicationMain + 159 23 PDCiPad 0x000000010cad8c1f 主 + 111 24 libdyld.dylib 0x0000000111a3268d 开始 + 1 ) libc++abi.dylib:以 NSException 类型的未捕获异常终止
最佳答案
当您点击 UIBarButtonItem
时,selectedRow
为 nil
因为您使用了 indexPathForSelectedRow
而您没有选择任意行。
override func prepare(for segue: UIStoryboardSegue, sender: Any?)
如果您点击您的 UIBarButtonItem
或 tableView 单元格,将被调用。
所以当您单击 tableView 单元格时它会起作用,因为 self.tableView.indexPathForSelectedRow
将返回所选单元格的索引。但是将为您的 UIBarButtonItem
返回 nil
。
最后,如果您在 nil 值上使用 !
,您会崩溃。
关于swift - 尝试执行 segue 导致 "terminating with uncaught exception of type NSException",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43034475/