nstextfield - NSTextView keyDown Event -


i want trigger keydown event every key action. therefore have created subclass of nsview.

@interface codrtextview : nsview {     nstextfield *linenumberspace;     nstextview *maintextview; }  - (id) initwithtextview:(nstextview *)textview linenumberspace:(nstextfield *)textfield; 

i have method's:

- (id) initwithtextview:(nstextview *)textview linenumberspace:(nstextfield *)textfield {    self = [self init];    if (self){        maintextview = textview;        linenumberspace = textfield;    }    return self; }  - (bool) acceptsfirstresponder {    return yes; }  - (bool)canbecomekeyview {    return  yes; } 

my plan count lines in textview , write numbers in linenumberspace. know methods work, because have test ibaction on button. method:

- (long) getlinecount{     nsstring *content = [maintextview string];     nsuinteger numberoflines, index, contentlength = [content length];     (index = 0, numberoflines = 0; index < contentlength; numberoflines++){          index = nsmaxrange([content linerangeforrange:nsmakerange(index, 0)]);     }      nslayoutmanager *layoutmanager = [maintextview layoutmanager];     nsuinteger numberofglyphs =[layoutmanager numberofglyphs];     nsrange linerange;     (numberoflines = 0, index = 0; index < numberofglyphs; numberoflines++){          (void) [layoutmanager linefragmentrectforglyphatindex:index                                            effectiverange:&linerange];           index = nsmaxrange(linerange);     }     numberoflines++;     return numberoflines; }  - (void)keydown:(nsevent *)event {      nsmutablestring *linenumberstring = [nsmutablestring string];      long numberoflines = [self getlinecount];       (int = 1; <= numberoflines; i++){          [linenumberstring appendstring:([nsstring stringwithformat:@"%d \n", i])];      }      [linenumberspace setstringvalue:linenumberstring]; } 

this method works surely. problem is, button number in linenumberspace changing correctly keydown event don't work. mistake here?

ahhh, looking @ code realize problem is.

your "codrtextview" object not subclassed "nstextview" (and also, when instantiate object programatically or via xib or storyboard, make sure text view object custom class of "codrtextview" , not "nstextview"), it's not getting "acceptsfirstresponder" or "canbecomekeyview" method calls either.

you need descend "codrtextview" "nstextview" instead of "nsview", or need create subclassed "nstextview" object will receive "keydown:" event , it'll call code calculates string goes "linenumberspace" of main view.

does make sense now?


Comments