如何修复错误:此类不符合键 tableView 的键值编码. [英] How to fix Error: this class is not key value coding-compliant for the key tableView.'

查看:24
本文介绍了如何修复错误:此类不符合键 tableView 的键值编码.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用 Table ViewSegmented Control 制作了一个应用程序,这是我的第一次.我正在使用一些代码和一些教程,但它不起作用.当我运行我的应用程序时,它崩溃了,并在日志中显示此错误:

I made an app with Table View and Segmented Control, and this is my first time. I'm using some code and some tutorials, but It's not working. When I run my app It's crashing and it's showing this Error in logs:

MyApplication[4928:336085] * 由于未捕获的异常NSUnknownKeyException"而终止应用程序,原因:[setValue:forUndefinedKey:]:该类不符合键 tableView 的键值编码."* 首先抛出调用栈:(0 核心基金会 0x000000010516fd85 __exceptionPreprocess + 1651 libobjc.A.dylib 0x0000000105504deb objc_exception_throw + 482 CoreFoundation 0x000000010516f9c9 -[NSException raise] + 93 基础 0x000000010364e19b -[NSObject(NSKeyValueCoding) setValue:forKey:] + 2884 UIKit 0x0000000103c37d0c -[UIViewController setValue:forKey:] + 885 UIKit 0x0000000103e6e7fb -[UIRuntimeOutletConnection 连接] + 1096 CoreFoundation 0x00000001050a9890 -[NSArray makeObjectsPerformSelector:] + 2247 UIKit 0x0000000103e6d1de -[UINib instantiateWithOwner:options:] + 18648 UIKit 0x0000000103c3e8d6 -[UIViewController _loadViewFromNibNamed:bundle:] + 3819 UIKit 0x0000000103c3f202 -[UIViewController loadView] + 17810 UIKit 0x0000000103c3f560 -[UIViewController loadViewIfRequired] + 13811 UIKit 0x0000000103c3fcd3 -[UIViewController 视图] + 2712 UIKit 0x000000010440b024 -[_UIFullscreenPresentationController _setPresentedViewController:] + 8713 UIKit 0x0000000103c0f5ca -[UIPresentationController initWithPresentedViewController:presentingViewController:] + 13314 UIKit 0x0000000103c525bb -[UIViewController _presentViewController:withAnimationController:completion:] + 400215 UIKit 0x0000000103c5585c -[UIViewController _performCoordinatedPresentOrDismiss:animated:] + 48916 UIKit 0x0000000103c5536b -[UIViewController presentViewController:animated:completion:] + 17917 UIKit 0x00000001041feb8d __67-[UIStoryboardModalSegueTemplate newDefaultPerformHandlerForSegue:]_block_invoke + 24318 UIKit 0x00000001041ec630 -[UIStoryboardSegueTemplate _performWithDestinationViewController:sender:] + 46019 UIKit 0x00000001041ec433 -[UIStoryboardSegueTemplate _perform:] + 8220 UIKit 0x00000001041ec6f7 -[UIStoryboardSegueTemplate 执行:] + 15621 UIKit 0x0000000103aa6a8d -[UIApplication sendAction:to:from:forEvent:] + 9222 UIKit 0x0000000103c19e67 -[UIControl sendAction:to:forEvent:] + 6723 UIKit 0x0000000103c1a143 -[UIControl _sendActionsForEvents:withEvent:] + 32724 UIKit 0x0000000103c19263 -[UIControl touchesEnded:withEvent:] + 60125 UIKit 0x0000000103b1999f -[UIWindow _sendTouchesForEvent:] + 83526 UIKit 0x0000000103b1a6d4 -[UIWindow sendEvent:] + 86527 UIKit 0x0000000103ac5dc6 -[UIApplication sendEvent:] + 26328 UIKit 0x0000000103a9f553 _UIApplicationHandleEventQueue + 666029 核心基金会 0x0000000105095301 _CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION_ + 1730 核心基金会 0x000000010508b22c __CFRunLoopDoSources0 + 55631 核心基金会 0x000000010508a6e3 __CFRunLoopRun + 86732 核心基金会 0x000000010508a0f8 CFRunLoopRunSpecific + 48833 图形服务 0x000000010726dad2 GSEventRunModal + 16134 UIKit 0x0000000103aa4f09 UIApplicationMain + 17135 迪克尔 0x0000000101f26282 主 + 11436 libdyld.dylib 0x00000001064c392d 开始 + 1)libc++abi.dylib:以未捕获的 NSException 类型异常终止

MyApplication[4928:336085] * Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key tableView.' * First throw call stack: ( 0 CoreFoundation 0x000000010516fd85 __exceptionPreprocess + 165 1 libobjc.A.dylib 0x0000000105504deb objc_exception_throw + 48 2 CoreFoundation 0x000000010516f9c9 -[NSException raise] + 9 3 Foundation 0x000000010364e19b -[NSObject(NSKeyValueCoding) setValue:forKey:] + 288 4 UIKit 0x0000000103c37d0c -[UIViewController setValue:forKey:] + 88 5 UIKit 0x0000000103e6e7fb -[UIRuntimeOutletConnection connect] + 109 6 CoreFoundation 0x00000001050a9890 -[NSArray makeObjectsPerformSelector:] + 224 7 UIKit 0x0000000103e6d1de -[UINib instantiateWithOwner:options:] + 1864 8 UIKit 0x0000000103c3e8d6 -[UIViewController _loadViewFromNibNamed:bundle:] + 381 9 UIKit 0x0000000103c3f202 -[UIViewController loadView] + 178 10 UIKit 0x0000000103c3f560 -[UIViewController loadViewIfRequired] + 138 11 UIKit 0x0000000103c3fcd3 -[UIViewController view] + 27 12 UIKit 0x000000010440b024 -[_UIFullscreenPresentationController _setPresentedViewController:] + 87 13 UIKit 0x0000000103c0f5ca -[UIPresentationController initWithPresentedViewController:presentingViewController:] + 133 14 UIKit 0x0000000103c525bb -[UIViewController _presentViewController:withAnimationController:completion:] + 4002 15 UIKit 0x0000000103c5585c -[UIViewController _performCoordinatedPresentOrDismiss:animated:] + 489 16 UIKit 0x0000000103c5536b -[UIViewController presentViewController:animated:completion:] + 179 17 UIKit 0x00000001041feb8d __67-[UIStoryboardModalSegueTemplate newDefaultPerformHandlerForSegue:]_block_invoke + 243 18 UIKit 0x00000001041ec630 -[UIStoryboardSegueTemplate _performWithDestinationViewController:sender:] + 460 19 UIKit 0x00000001041ec433 -[UIStoryboardSegueTemplate _perform:] + 82 20 UIKit 0x00000001041ec6f7 -[UIStoryboardSegueTemplate perform:] + 156 21 UIKit 0x0000000103aa6a8d -[UIApplication sendAction:to:from:forEvent:] + 92 22 UIKit 0x0000000103c19e67 -[UIControl sendAction:to:forEvent:] + 67 23 UIKit 0x0000000103c1a143 -[UIControl _sendActionsForEvents:withEvent:] + 327 24 UIKit 0x0000000103c19263 -[UIControl touchesEnded:withEvent:] + 601 25 UIKit 0x0000000103b1999f -[UIWindow _sendTouchesForEvent:] + 835 26 UIKit 0x0000000103b1a6d4 -[UIWindow sendEvent:] + 865 27 UIKit 0x0000000103ac5dc6 -[UIApplication sendEvent:] + 263 28 UIKit 0x0000000103a9f553 _UIApplicationHandleEventQueue + 6660 29 CoreFoundation 0x0000000105095301 _CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION_ + 17 30 CoreFoundation 0x000000010508b22c __CFRunLoopDoSources0 + 556 31 CoreFoundation 0x000000010508a6e3 __CFRunLoopRun + 867 32 CoreFoundation 0x000000010508a0f8 CFRunLoopRunSpecific + 488 33 GraphicsServices 0x000000010726dad2 GSEventRunModal + 161 34 UIKit 0x0000000103aa4f09 UIApplicationMain + 171 35 Dhikr 0x0000000101f26282 main + 114 36 libdyld.dylib 0x00000001064c392d start + 1 ) libc++abi.dylib: terminating with uncaught exception of type NSException

我使用的代码是:

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

let foodList:[String] = ["Bread", "Meat", "Pizza", "Other"]
let drinkList:[String] = ["Water", "Soda", "Juice", "Other"]

@IBOutlet weak var mySegmentedControl: UISegmentedControl!
@IBOutlet weak var myTableView: UITableView!

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    var returnValue = 0

    switch(mySegmentedControl.selectedSegmentIndex) {
    case 0:
        returnValue = foodList.count
        break
    case 1:
        returnValue = drinkList.count
        break
    default:
        break
    }

    return returnValue
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let myCell = tableView.dequeueReusableCellWithIdentifier("myCells", forIndexPath: indexPath)

    switch(mySegmentedControl.selectedSegmentIndex) {
    case 0:
        myCell.textLabel!.text = foodList[indexPath.row]
        break
    case 1:
        myCell.textLabel!.text = drinkList[indexPath.row]
        break
    default:
        break
    }

    return myCell
}

@IBAction func segmentedControlActionChanged(sender: AnyObject) {
    myTableView.reloadData()  
}

这是ma​​in.Storyboard

我多次检查代码,但它不起作用.首先,我必须只使用 Table View,观看本教程 (https://www.youtube.com/watch?v=ABVLSF3Vqdg) 我认为在教程中使用 Segmented Control 会起作用.但仍然不起作用.同样的代码,同样的错误.有人可以帮我吗?

I checked the code many times, but it's not working. First I had to use only Table View, watching this tutorial (https://www.youtube.com/watch?v=ABVLSF3Vqdg) I thought it will work to use Segmented Control as in tutorial. But still doesn't work. Same code, same error. Can someone help me ?

推荐答案

您已将故事板设置为期待名为 tableView 的插座,但实际插座名称为 myTableView.

You have your storyboard set up to expect an outlet called tableView but the actual outlet name is myTableView.

如果您删除情节提要中的连接并重新连接到正确的变量名称,则应该可以解决问题.

If you delete the connection in the storyboard and reconnect to the right variable name, it should fix the problem.

这篇关于如何修复错误:此类不符合键 tableView 的键值编码.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆