2015-06-10 103 views
-1

我爲我的ios應用程序創建了一個幻燈片抽屜..幻燈片抽屜工作完美...我有幾個按鈕和幻燈片抽屜中的文本框...問題是每當我嘗試添加引用在幻燈片抽屜中的按鈕/文本框,我得到NSUnknownKeyException ...我不知道如何解決這個問題,因爲我是非常新的IO ...包括下面的代碼...NSUnknownKeyException ios Objective C

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 

EDIT

異常是在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的CoreFoundation
0x0089e361 - [NSException提高] + 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 - [UIRuntimeOutletConnectio n連接] + 106 8
libobjc.A。dylib 0x0053d724 - [NSObject的 performSelector:] + 62 9的CoreFoundation
0x007d647c - [NSArray的makeObjectsPerformSelector:] + 316 10的UIKit
0x00fa7133 - [UINib instantiateWithOwner:選擇:] + 1775 11的UIKit
0x00fa8fd2 - [一個NSBundle(UINSBundleAdditions) loadNibNamed:所有者:選擇:] + 180個12 Xpressions的
0x000418cf - [mainViewController viewDidLoad中] + 175 13的UIKit
0x00dc1da4 - [UIViewController中loadViewIfRequired] + 771 14的UIKit
0x00dc2095 - [UIViewController的視圖] + 35 15的UIKit
0x00cb3e85 - [UIWindow addRootViewContr ollerViewIfPossible] + 66 16 的UIKit 0x00cb434c - [一個UIWindow _setHidden:強制:] + 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:完成: ] + 1639 23 UIKit 0x00c7493e __84- [UIApplication _handleApplicationActivationWithScene:transitionContext:完成:] _ 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個FrontBoardServices 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的CoreFoundation
0x007b4a5b CFRunLoopRunSpecific + 443 32的CoreFoundation
0x007b488b CFRunLoopRunInMode + 123 33的UIKit
0x00c59a02 - [UIApplication的_run] + 571 34的UIKit
0x00c5d106 UIApplicationMain + 1526 35 Xpressions的
0x00043a0a主+ 138 36 libdyld.dylib
0x02c2aac9開始+ 1)的libC++ abi.dylib:與未捕獲 異常類型NSException的

+1

你得到的例外是什麼?另外,在Xcode中添加一個異常斷點,這樣你就可以看到哪一行導致異常被拋出。 –

+0

請檢查修改過的問題... –

+1

您正在使用xib,對嗎? – Miknash

回答

5

這有兩個可能的原因:

容易,你有連接,刪除元素,但鏈接的出口和代碼之間保持,現在指向空。您可以在界面構建器右側的最後一個選項卡中查看這一個。

另一個:你已經連接你的插座文件所有者,而不是班級。取下你的插座並將它們連接到班級。當你連接你的插座(從代碼到元素,你將有空圈),你將得到選擇的彈出窗口:文件所有者 - 類。

編輯:

按x鍵取出插座,然後再次從代碼中重新連接。當您刪除這些插座時,您將在.h類中有空圈並再次重新連接到SlideDrawer。

enter image description here

+0

你能爲我提供第二個解決方案的截圖嗎? –

+0

好吧,它很難做到只有圖像,但它很容易遵循以下步驟:打開SlideDrawer.xib - >去商店,按實心圓去刪除網點 - >打開SlideDrawer.h - >你將有空的圓圈現在 - >將它們連接到相應的元素 - >選擇SlideDrawer而不是文件所有者 – Miknash

+0

很抱歉說這個..但是我無法在SlideDrawer.h中找到任何空的電路...... :( –

0

終止
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