0

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.

Robby
  • 23
  • 8

1 Answers1

0

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. :->

bhaller
  • 1,803
  • 15
  • 24
  • Thank you. It´s really interesting: I wrote a little method with a button for hiding it. The hiding works, but not the appearing. Here it is: `-(IBAction)hide:(id)sender{ [testTextField setHidden:YES]; }` The same for appearing: `-(IBAction)show:(id)sender{ [testTextField setHidden:NO]; }`. Both methods work, but not with `becomeFirstResponder`. – Robby Nov 16 '15 at 22:50
  • Huh, I don't know why that would be. Well, the usual fix for that situation – where calling a method like `setHidden:` doesn't do what you want because the timing is somehow bad – is to call it instead as a delayed perform. So try `[testTextFiel performSelector:@selector(setHidden:) withObject:nil afterDelay:0.0];` and see if that helps. The `setHidden:` method takes a `BOOL`, not an object, but passing `nil` should be interpreted as passing a `NO` value, I think, since both represented by zero; but if that is too hackish for you, call a method on yourself that then calls `setHidden:`. – bhaller Nov 17 '15 at 02:39
  • Hmm...it still doesn't work. Perhaps you can take directly a look at my project ? I posted it on GitHub. Here is the link: https://github.com/Robert-T9911/NSTextField-test – Robby Nov 17 '15 at 09:18
  • In your `becomeFirstResponder` method, I added `NSLog(@"TextField1: %@", TextField1);`, as I suggested above that you ought to do to check that the ivar was correctly set up; that printed that `TextField1` is nil. Examining your nib, you appear to have two objects, both named "My Text Field". The one that is visible has no connection to its TextField1 outlet. The one that is not visible – just a free object in the nib – has a connection to its TextField1 outlet, but since it is not visible, that does not matter. So you need to delete the extra textfield and fix your nib connections. – bhaller Nov 17 '15 at 14:42
  • Thank you ;) The appearing works now but I would also like to make the NSTextField disappear when the Focus gets lost. So I wrote the method `-(BOOL)resignFirstResponder{ NSLog(@"focus get lost"); [TextField1 setHidden:YES]; return YES; }`. According to the debugger area both methods are executed at the same time. Why happens this ? How can I avoid this and code it right ? – Robby Nov 17 '15 at 18:36
  • `textDidEndEditing:` might work better for your purposes. But mostly I'd recommend more debugger work to figure out what is going on. – bhaller Nov 17 '15 at 21:03
  • One idea as to why resignFirstResponder is not working well for you is: when textfield1 gets focus, textfield2 is shown, and perhaps that makes textfield2 receive first responder status, which would make textfield1 resign first responder, which would make textfield2 disappear again. Even if that is not the bug, it seems like a problem in your design; the user will not be able to use textfield2 since it will disappear as soon as the user clicks in it. Also, in any case, it would be appreciated if you would accept this answer, since I found your bug for you... – bhaller Nov 17 '15 at 22:43