【问题标题】:NSUnknownKeyException ios Objective C [duplicate]NSUnknownKeyException ios Objective C [重复]
【发布时间】:2015-08-25 10:17:08
【问题描述】:

我为我的 ios 应用程序创建了一个滑动抽屉。滑动抽屉运行良好……我在滑动抽屉中有几个按钮和一个文本框……问题是每当我尝试添加对按钮的引用时/textbox 在滑动抽屉中,我收到 NSUnknownKeyException ...我不知道如何解决这个问题,因为我在 IO 中非常新...我包括下面的代码...

SlideDrawer.h

#import <UIKit/UIKit.h>

@interface SlideDrawer : UIViewController
@property (strong, nonatomic) IBOutlet UIButton *home;
- (IBAction)homeclick:(id)sender;
@end

SlideDrawer.m

#import "SlideDrawer.h"
@interface SlideDrawer ()
@end
@implementation SlideDrawer
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
 - (IBAction)homeclick:(id)sender {
}
@end

MainViewController.h

#import <UIKit/UIKit.h>

@interface mainViewController : UIViewController{
UIView *menuDrawer;
}

@property (readonly, nonatomic) UISwipeGestureRecognizer     *recognizer_open, *recognizer_close;
@property (readonly, nonatomic) int menuDrawerX, menuDrawerWidth;

-(void) handleSwipes:(UIGestureRecognizer *) sender;
-(void) drawerAnimation;

@end

MainViewController.m

#import "mainViewController.h"

@interface mainViewController ()

@end

@implementation mainViewController

@synthesize menuDrawerWidth, menuDrawerX,recognizer_open, recognizer_close;

 - (void)viewDidLoad {
    [super viewDidLoad];
    menuDrawer = [[[NSBundle mainBundle] loadNibNamed:@"SlideDrawer" owner:self options:nil]objectAtIndex:0];
    menuDrawer.backgroundColor= [UIColor colorWithRed:0.18 green:0.09 blue:0.29 alpha:1.0];
    menuDrawerWidth= self.view.frame.size.width * 0.80;
    int statusbarHeight= [UIApplication sharedApplication].statusBarFrame.size.height;
    menuDrawerX= self.view.frame.origin.x- menuDrawerWidth;
    menuDrawer.frame= CGRectMake(menuDrawerX,  menuDrawer.frame.origin.y+statusbarHeight, menuDrawerWidth, self.view.frame.size.height-statusbarHeight);
    // menuDrawer.backgroundColor=[UIColor colorWithRed:0.18 green:0.09 blue:0.29 alpha:1.0];
    recognizer_close= [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipes:)];
    recognizer_open= [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipes:)];
    recognizer_close.direction= UISwipeGestureRecognizerDirectionLeft;
    recognizer_open.direction= UISwipeGestureRecognizerDirectionRight;
    [self.view addGestureRecognizer:recognizer_open];
    [self.view addGestureRecognizer:recognizer_close];
    [self.view addSubview:menuDrawer];
}

- (void)didReceiveMemoryWarning {
     [super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
 }

 -(void) handleSwipes:(UIGestureRecognizer *) swipe{
     [self drawerAnimation];

}
-(void) drawerAnimation{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDuration:-10];
    CGFloat new_x = 0;
    if(menuDrawer.frame.origin.x<self.view.frame.origin.x)
    {
        new_x= menuDrawer.frame.origin.x+menuDrawerWidth;
        UIBezierPath *shadowPath = [UIBezierPath bezierPathWithRect:menuDrawer.bounds];
        menuDrawer.layer.masksToBounds = NO;
        menuDrawer.layer.shadowColor = [UIColor blackColor].CGColor;
        menuDrawer.layer.shadowOffset = CGSizeMake(0.0f, 1.0f);
        menuDrawer.layer.shadowOpacity = 0.5f;
        menuDrawer.layer.shadowPath = shadowPath.CGPath;
        [self.view removeGestureRecognizer:recognizer_open];
        [self.view addGestureRecognizer:recognizer_close];
    }
    else{
        new_x= menuDrawer.frame.origin.x-menuDrawerWidth;
        UIBezierPath *shadowPath = [UIBezierPath bezierPathWithRect:menuDrawer.bounds];
        menuDrawer.layer.masksToBounds = NO;
        menuDrawer.layer.shadowColor = [UIColor blackColor].CGColor;
        menuDrawer.layer.shadowOffset = CGSizeMake(0.0f, 1.0f);
        menuDrawer.layer.shadowOpacity = 0.0f;
        menuDrawer.layer.shadowPath = shadowPath.CGPath;
        [self.view removeGestureRecognizer:recognizer_close];
        [self.view addGestureRecognizer:recognizer_open];
    } 
    menuDrawer.frame= CGRectMake(new_x, menuDrawer.frame.origin.y, menuDrawer.frame.size.width, menuDrawer.frame.size.height);
    [UIView commitAnimations];
 } 
 @end

编辑

异常是 main.m 中的行

 return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));

例外

2015-06-10 14:07:17.357 Xpressions[5939:1056267] Unknown class ViewController in Interface Builder file. 2015-06-10 14:07:17.367

Xpressions[5939:1056267] * 由于未捕获的异常而终止应用程序 'NSUnknownKeyException',原因:'[ setValue:forUndefinedKey:]:这个类不是键值 符合关键家庭的编码标准。 * 第一次抛出调用堆栈:( 0 CoreFoundation 0x0089e746 exceptionPreprocess + 182 1 libobjc.A.dylib
0x00527a97 objc_exception_throw + 44 2 核心基础
0x0089e361 -[NSException raise] + 17 3 基础
0x001a81ee -[NSObject(NSKeyValueCoding) setValue:forUndefinedKey:] + 282 4 基础 0x00105608 _NSSetUsingKeyValueSetter + 115 5 基础 0x0010558d -[NSObject(NSKeyValueCoding) setValue:forKey:] + 267 6
基础 0x0013b016 -[NSObject(NSKeyValueCoding) setValue:forKeyPath:] + 386 7 UIKit 0x00fa8672 -[UIRuntimeOutletConnection 连接] + 106 8
libobjc.A.dylib 0x0053d724 -[NSObject performSelector:] + 62 9 核心基础
0x007d647c -[NSArray makeObjectsPerformSelector:] + 316 10 UIKit
0x00fa7133 -[UINib instantiateWithOwner:options:] + 1775 11 UIKit
0x00fa8fd2 -[NSBundle(UISBundleAdditions) loadNibNamed:owner:options:] + 180 12 Xpressions
0x000418cf -[mainViewController viewDidLoad] + 175 13 UIKit
0x00dc1da4 -[UIViewController loadViewIfRequired] + 771 14 UIKit
0x00dc2095 -[UIViewController 视图] + 35 15 UIKit
0x00cb3e85 -[UIWindow addRootViewControllerViewIfPossible] + 66 16 UIKit 0x00cb434c -[UIWindow _setHidden:forced:] + 287 17 UIKit 0x00cb4648 -[UIWindow _orderFrontWithoutMakingKey] + 49 18 UIKit
0x00cc29b6 -[UIWindow makeKeyAndVisible] + 80 19 Xpressions
0x00043534 -[AppDelegate 应用程序:didFinishLaunchingWithOptions:] + 580 20 UIKit 0x00c56fd7 -[UIApplication _handleDelegateCallbacksWithOptions:isSuspended:restoreState:] + 287 21 UIKit 0x00c57dc1 -[UIApplication _callInitializationDelegatesForMainScene:transitionContext:] + 2938 22 UIKit 0x00c5b422 -[UIApplication _runWithMainScene:transitionContext:completion:] + 1639 23 UIKit 0x00c7493e __84-[UIApplication _handleApplicationActivationWithScene:transitionContext:completion:]_block_invoke + 59 24 UIKit 0x00c5a04a -[UIApplication workspaceDidEndTransaction:] + 155 25 FrontBoardServices 0x03200c9e __37-[FBSWorkspace clientEndTransaction:]_block_invoke_2 + 71 26 FrontBoardServices
0x0320072f __40-[FBSWorkspace _performDelegateCallOut:]_block_invoke + 54 27 前板服务 0x03212d7c __31-[FBSSerialQueue performAsync:]_block_invoke_2 + 30 28 CoreFoundation 0x007c0050 __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK
+ 16 29 CoreFoundation 0x007b5963 __CFRunLoopDoBlocks + 195 30 CoreFoundation
0x007b57bb __CFRunLoopRun + 2715 31 核心基础
0x007b4a5b CFRunLoopRunSpecific + 443 32 核心基础
0x007b488b CFRunLoopRunInMode + 123 33 UIKit
0x00c59a02 -[UIApplication_run] + 571 34 UIKit
0x00c5d106 UIApplicationMain + 1526 35 Xpressions
0x00043a0a 主 + 138 36 libdyld.dylib
0x02c2aac9 start + 1 ) libc++abi.dylib: 以未捕获的方式终止 NSException 类型的异常

【问题讨论】:

  • 你得到什么异常?另外,在 Xcode 中添加一个异常断点,这样你就可以看到是哪一行导致了异常被抛出。
  • 请检查已编辑的问题...
  • 你用的是xib,对吧?
  • yaa...你是对的

标签: ios objective-c nsunknownkeyexception


【解决方案1】:

解决方案

STEP 1:Go XIB or if use STORYBOARD go STORYBOARD
STEP 2:Click View Controller
STEP 3:Again Give connection properly(Fill the circle)
STEP 4:Also check whether it is for button action or outlet property

非常重要: 右键单击文件的所有者(黄色)和 单击“连接检查器”(右上箭头),一次查看所有插座。查找 !s 表示缺少插座。删除它。 现在可以了。

请看下面的参考资料

  1. What does this mean? "'NSUnknownKeyException', reason: … this class is not key value coding-compliant for the key X"
  2. setValue:forUndefinedKey: this class is not key value coding-compliant for the key

  3. What does this mean? "'NSUnknownKeyException', reason: … this class is not key value coding-compliant for the key X"

  4. Class is not key value coding-compliant

【讨论】:

    【解决方案2】:

    这有两个可能的原因:

    简单,你有连接,删除元素但出口和代码之间的链接仍然存在,现在指向空。您可以在界面构建器右侧的最后一个选项卡中查看此选项卡。

    另一个:您已将您的插座连接到文件所有者而不是类。删除您的插座并将它们连接到班级。当您连接插座时(从代码到元素,您将有一个空圆圈),您将获得选择的弹出窗口:文件所有者 - 类。

    编辑:

    按 x 删除插座,然后从代码重新连接。当您删除这些插座时,您将在 .h 类中有空圆圈并重新连接到 SlideDrawer。

    【讨论】:

    • 能否提供第二个解决方案的屏幕截图?
    • 好吧,仅使用图像很难做到这一点,但很容易按照以下步骤操作:打开 SlideDrawer.xib -> 转到插座,按实心圆圈以删除插座 -> 打开 SlideDrawer.h -> 你现在会有空圆圈 -> 将它们连接到相应的元素 -> 选择 SlideDrawer 而不是 File Owner
    • 很抱歉这么说.. 但我在 SlideDrawer.h 中找不到任何空圆圈... :(
    • 您是否从 xib 中删除了网点?如果您从界面构建器中删除它们,则必须至少有一个主页按钮
    • 更新截图
    猜你喜欢
    • 2011-06-14
    • 2012-06-05
    • 1970-01-01
    • 2014-08-31
    • 2013-12-12
    • 1970-01-01
    • 2020-07-31
    • 1970-01-01
    • 2023-04-05
    相关资源
    最近更新 更多