【问题标题】:iOS this class is not key value coding-compliant for the key labeliOS 此类不符合键标签的键值编码
【发布时间】:2014-08-06 10:35:00
【问题描述】:

所以我是 iOS 编程新手,但遇到了问题。

我创建了一个新的视图控制器,然后单击复选标记自动为其创建一个 .xib 文件。在 xib 文件中,我放了一个标签,我想淡入。所以我尝试将它链接到我的自定义视图控制器,并不断崩溃,因为“这个类不符合键 login1 的键值编码”。 Login1 是我导入的 UILabel 的名称。这是 RootView 的头文件:

@interface RootViewController : UIViewController {
    PlayerStatViewController *playerStatViewController;
    NSString *viewName;
}
@property (weak, nonatomic) IBOutlet UILabel *login1;
@property (nonatomic, retain) PlayerStatViewController *playerStatViewController;
@property (nonatomic, retain) NSString *viewName;

-(IBAction)switchPage:(id)sender:(NSString *)toSwitch;
-(void) animateButton;
@end

还有 .m 文件: #import "RootViewController.h" #import "FileUtils.h"

@interface RootViewController ()

@end

@implementation RootViewController

@synthesize playerStatViewController;
@synthesize viewName;
@synthesize login1;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
    }
    self.viewName = @"Boot";
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSString* fpath = [NSString stringWithFormat:@"%@%@", getDefPath(), @"/config.txt"];
    self.login1.text = loadfile(@"serverName", fpath);
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

-(IBAction)switchPage:(id)sender :(NSString *)toSwitch {
    if([toSwitch isEqualToString:(@"PlayerStat")]) {
        if(self.playerStatViewController == nil) {
        PlayerStatViewController *playerStatView = [[PlayerStatViewController alloc]
                                                    initWithNibName:(@"PlayerStat") bundle:[NSBundle mainBundle]];
            self.playerStatViewController = playerStatView;
        }
    [self.navigationController pushViewController:self.playerStatViewController animated:YES];
    }else if([toSwitch isEqualToString:(@"Boot")]) {
        [self.navigationController pushViewController:self animated:YES];
    }
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [self performSelector:@selector(animateButton) withObject:nil afterDelay:0.1f];
}

- (void)animateButton {
    self.login1.alpha = 0;

    [UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionCurveEaseIn
                     animations:^{ self.login1.alpha = 1;}
                      completion:nil];
}

@end

我如何加载 Xib(如果重要的话):

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        RootViewController *viewController = [[RootViewController alloc] initWithNibName:@"RootViewController" bundle:nil];
        [self.window setRootViewController:viewController];
        [window makeKeyAndVisible];
        return YES;
    }

项目仅针对 iPhone 构建。 在 Xib 中,是的,自定义视图控制器被设置为文件所有者。没有错误指向的值(检查管理器中的感叹号)。

编辑: 崩溃日志:

    2014-08-06 06:02:12.887 TrinityApi[5761:60b] *** Terminating app due to uncaught exception     'NSUnknownKeyException', reason: '[<UIApplication 0x8c781d0> setValue:forUndefinedKey:]: this     class is not key value coding-compliant for the key login1.'
*** First throw call stack:
(
    0   CoreFoundation                      0x017ee1e4 __exceptionPreprocess + 180
    1   libobjc.A.dylib                     0x0156d8e5 objc_exception_throw + 44
    2   CoreFoundation                      0x0187dfe1 -[NSException raise] + 17
    3   Foundation                          0x0122dd9e -[NSObject(NSKeyValueCoding) setValue:forUndefinedKey:] + 282
    4   Foundation                          0x0119a1d7 _NSSetUsingKeyValueSetter + 88
    5   Foundation                          0x01199731 -[NSObject(NSKeyValueCoding) setValue:forKey:] + 267
    6   Foundation                          0x011fbb0a -[NSObject(NSKeyValueCoding) setValue:forKeyPath:] + 412
    7   UIKit                               0x004e41f4 -[UIRuntimeOutletConnection connect] + 106
    8   libobjc.A.dylib                     0x0157f7de -[NSObject performSelector:] + 62
    9   CoreFoundation                      0x017e976a -[NSArray makeObjectsPerformSelector:] + 314
    10  UIKit                               0x004e2d4d -[UINib instantiateWithOwner:options:] + 1417
    11  UIKit                               0x004e4ada -[NSBundle(UINSBundleAdditions) loadNibNamed:owner:options:] + 165
    12  UIKit                               0x0022d61b -[UIApplication _loadMainNibFileNamed:bundle:] + 58
    13  UIKit                               0x0022d949 -[UIApplication _loadMainInterfaceFile] + 245
    14  UIKit                               0x0022c54e -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 543
    15  UIKit                               0x00240f92 -[UIApplication handleEvent:withNewEvent:] + 3517
    16  UIKit                               0x00241555 -[UIApplication sendEvent:] + 85
    17  UIKit                               0x0022e250 _UIApplicationHandleEvent + 683
    18  GraphicsServices                    0x037e3f02 _PurpleEventCallback + 776
    19  GraphicsServices                    0x037e3a0d PurpleEventCallback + 46
    20  CoreFoundation                      0x01769ca5 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 53
    21  CoreFoundation                      0x017699db __CFRunLoopDoSource1 + 523
    22  CoreFoundation                      0x0179468c __CFRunLoopRun + 2156
    23  CoreFoundation                      0x017939d3 CFRunLoopRunSpecific + 467
    24  CoreFoundation                      0x017937eb CFRunLoopRunInMode + 123
    25  UIKit                               0x0022bd9c -[UIApplication _run] + 840
    26  UIKit                               0x0022df9b UIApplicationMain + 1225
    27  TrinityApi                          0x00002a9d main + 141
    28  libdyld.dylib                       0x01e35701 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

编辑:只是随机开始工作

【问题讨论】:

  • 这可能有助于显示崩溃发生的位置。要确定确切的行或位置,建议您为异常添加一个断点,并在发生崩溃时查看它在调试器中停止的位置。
  • 能否提供崩溃日志
  • 编辑有错误日志。并且 volker 它在特定行崩溃: return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
  • 检查您的用户定义的运行时属性。如果您更改了控制类,那些(缺失的)属性可能是问题所在。

标签: ios objective-c nsunknownkeyexception


【解决方案1】:

"class is not key value coding-compliant" 表示您最初为我们的标签分配了一个插座并已将其连接到我们的 xib 中,但随后您删除了该插座并且 xib 是仍然包含对不再存在的插座的引用,您需要从 xib 中删除此引用。

转到您的XIB文件并右键单击Files Owner,您可能会发现一个带有黄色标记的标签,只需删除其连接的插座,再次清理并运行您的程序

【讨论】:

  • 如前所述,连接正确。任何网点旁边都没有“黄色标记”或“感叹号”。
  • 那么我可能需要看看你的项目,因为这是与 xib 插座及其连接相关的错误
  • 这里我会压缩它,然后在线发布。
  • 好的,它刚刚起作用 不知道那里发生了什么。没有更改任何内容或重新启动应用程序。
猜你喜欢
  • 2017-10-15
  • 1970-01-01
  • 1970-01-01
  • 2019-10-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多