I have a NSTextField which I subclassed from NSTextField and I want that a new NSTextField appears when my TextField gets focus. I changed the method becomeFirstResponder
, which also works, cause it prints "TextField got focus" in the log area.
But the appearance / disappearance doesn't work for my second TextField. Its also connected in the IB.
Here is the code:
@interface MyNewTextField : NSTextField{
IBOutlet NSTextField* TestTextFiel;
}
MyNewTextField.m:
#import "SollkontoFeld.h"
@implementation SollkontoFeld
- (void)drawRect:(NSRect)dirtyRect {
[super drawRect:dirtyRect];
}
-(BOOL)becomeFirstResponder{
[testTextFiel setHidden:NO];
NSLog(@"TextField got focus");
return YES;
}
@end
The methods is obviously correctly executed cause the text is printed. I assume that my code for the appearance / disappearance is false.
First of all, you should verify that the second textfield is in the right place, with the right layout constraints, etc., by having it start out as visible in the nib. Call setHidden:YES
to hide it somewhere convenient, like awakeFromNib
, so that it is not initially visible; but then you can just comment out that setHidden: call to have it visible on launch. My initial suspicion is that the setHidden:NO
call is working fine, but the textfield is not visible for some other reason.
Besides that, you just need to check that all your connections are good, objects are retained, etc. I notice that the code you posted declares an outlet named TestTextFiel
but then calls setHidden:
on testTextFiel
, which is not the same – case matters. You might just add an NSLog
of testTextFiel
in your becomeFirstResponder
method to verify that it exists; if it does, you could NSLog
its superview
; etc. Debug. Do some detective work to figure out what is wrong. After you call setHidden:NO
, the textfield should exist, have the right superview, have a sensical frame, return NO
to -hidden
, etc.; you can verify each of those things in the debugger. If it still doesn't draw, maybe you need a call to setNeedsDisplay:
, although I think that should not be necessary. Anyway, it's not really possible for us on stackoverflow to do your debugging work for you; that's a basic skill in programming that you need to learn. :->