0

I am trying to do a simple task:

I have an editable text field, two buttons (titles: make editable/ make un-editable) over a window. Idea is: when user clicks "make editable" button, text field should become editable and when he/she clicks "make un-editable", it should become un-editable.

In action of "make un-editable" I am doing this:

[myTextField setSelectable:NO];
[myTextField setEditable:NO];

and in action of "make editable" I am doing this:

[myTextField setSelectable:YES];
[myTextField setEditable:YES];

Problem is:

It works fine when myTextField does not have cursor within it or it has cursor but user does not type anything in it and clicks - "make un-editable", then myTextField becomes un-editable but when it has cursor and user clicks "make un-editable" after typing something within it he/she can still edit myTextField.

For its solution I tried to remove cursor from myTextField as soon as user clicks "make un-editable" button, by adding these lines before selectable and editable statements:

  1. [someOtherTextField selectText:self];

  2. [[NSRunLoop currentRunLoop] performSelector:@selector(selectText:) someOtherTextField argument:self order:9999 modes:[NSArray arrayWithObject:NSDefaultRunLoopMode]];

  3. [someOtherTextField becomeFirstResponder];

but none is working for me :(

Can anyone suggest some solution for it?

Kara
  • 6,115
  • 16
  • 50
  • 57
Devarshi
  • 16,440
  • 13
  • 72
  • 125

2 Answers2

5

The docs for becomeFirstResponder say

Use the NSWindow makeFirstResponder: method, not this method, to make an object the first responder. Never invoke this method directly.

I'm not sure if a hidden text field can become first responder, so try:

[[myTextField window] makeFirstResponder: nil]

JWWalker
  • 22,385
  • 6
  • 55
  • 76
0

in swift 5

use bellow code

 password.window?.makeFirstResponder(nil)
Siva Sankar
  • 543
  • 1
  • 4
  • 18