keyDown和NSTableViewCocoa

时间:2012-02-16 14:39:20

标签: cocoa exc-bad-access

我有一个Cocoa应用程序从NSScrollView获取输入,然后想要输出关于按下的键的一些信息,例如keyCode,modifier等。

问题是我在运行时收到EXC_BAD_ACCESS错误。运行时之前没有编译器错误。

该计划的代码如下:

keyboardModel.h

#import <Cocoa/Cocoa.h>


@interface keyboardModel : NSObject {

    NSString* charac;
    NSString* keyc;
    NSString* modif;

}


-(void) setValues: (NSString *) a :(NSString *)b :(NSString *) c;

@property (copy) NSString* charac;
@property (copy) NSString* keyc;
@property (copy) NSString* modif;

@end

keyboardModel.m

#import "keyboardModel.h"
#import "MyController.h"


@implementation keyboardModel

@synthesize charac;
@synthesize keyc;
@synthesize modif;

-(void) setValues: (NSString *) a :(NSString *)b :(NSString *) c{

    charac = [charac stringByAppendingString:a];
    keyc = [keyc stringByAppendingString:b];
    modif = [modif stringByAppendingString:c];  

}

-(id) init {
        self = [super init];

    if (self)
    {       
        charac = @"dddd";
        keyc = @"xxx";
        modif = @"xxx";
    }

    return self;
}


@end

MyController.h

#import <Cocoa/Cocoa.h>

@interface MyController : NSObject<NSTableViewDataSource>
{
    IBOutlet id typingArea; 
    IBOutlet NSTableView *tableView;
    NSMutableArray *list;
}

-(void)showKeyDownEvent:(NSEvent *)theEvent;

@end

MyController.m

#import "MyController.h"
#import "MyTextView.h"
#import "keyboardModel.h"

#import <Carbon/Carbon.h>

static const int INS_MOD_FLAG_OPTION_KEY = (optionKey >> 8) & 0xff;
static const int INS_MOD_FLAG_SHIFT_KEY = (shiftKey >> 8) & 0xff;
static const int INS_MOD_FLAG_CONTROL_KEY = (controlKey >> 8) & 0xff;
static const int INS_MOD_FLAG_ALPHA_LOCK = (alphaLock >> 8) & 0xff;
static const int INS_MOD_FLAG_CMD_KEY = (cmdKey >> 8) & 0xff;

@implementation MyController

- (id) init 
{
        self = [super init];
    if (self)
    {
        list = [[NSMutableArray alloc] init];

    }
    return self;
}


static NSString* print_mods(UInt32 unl_mods) {

    NSString *modifiers = [NSString stringWithFormat:@""];

    if(unl_mods & NSAlphaShiftKeyMask)

        modifiers = [NSString stringWithFormat:@"%@ Caps Lock", modifiers]; 

    if(unl_mods & NSShiftKeyMask)

        modifiers = [NSString stringWithFormat:@"%@ Shift Key", modifiers]; 

    if(unl_mods & NSControlKeyMask)

        modifiers = [NSString stringWithFormat:@"%@ Control Key", modifiers]; 

    if(unl_mods & NSAlternateKeyMask)

        modifiers = [NSString stringWithFormat:@"%@ Alt Key", modifiers]; 

    if (unl_mods == 384) 

        modifiers = [NSString stringWithFormat:@"No Modifier"]; 


    return modifiers;
} 

-(void)showKeyDownEvent:(NSEvent *)e
{

    //%x - hex value
    //%d - decimal value

    NSString* myNewString1 = [NSString stringWithFormat:@"%x(%d)", [e keyCode], [e keyCode]];
    NSString* myNewString2 = [NSString stringWithFormat:@"%d", [e modifierFlags]];


    NSString* flagReplace = [NSString stringWithFormat:@""];
    int value = [myNewString2 intValue];

    NSLog (@"%d",value);

    flagReplace = print_mods ([e modifierFlags]);

    NSString *temp1 =  [@"Character: " stringByAppendingString: [e characters]];
    NSString *temp2 =  [@" Keycode: "  stringByAppendingString: myNewString1];
    NSString *temp3 =  [@" Modifier: " stringByAppendingString: flagReplace];


    keyboardModel *km = [[keyboardModel alloc] init];
    [km setValues:temp1:temp2:temp3];
    [list addObject: km];

    [tableView reloadData];
}

- (id) tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row{
    keyboardModel *p = [list objectAtIndex:row];
    NSString* identifier = [tableColumn identifier];

    return [p valueForKey: identifier];
}

-(NSInteger) numberOfRowsInTableView : (NSTableView *) tableView {
    return [list count];
}

// closing the last window quits the app
- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)theApplication
{
    return YES;
}

@end

MyTextView.h

#import <Cocoa/Cocoa.h>

@interface MyTextView : NSTextView
{
   IBOutlet id controller;

}
@end

MyTextView.m

#import "MyTextView.h"
#import "MyController.h"

@implementation MyTextView

- (void)keyDown:(NSEvent *)theEvent
{
    [controller showKeyDownEvent: theEvent];
    [super keyDown: theEvent];
}

        @end

对于冗长的缠绕代码感到抱歉,但错误让我感到疯狂。谢谢你的帮助!

1 个答案:

答案 0 :(得分:0)

解决了,另一个类中的初始化方法存在问题。