Selecting text programmatically in menu view
I write an "Agent" Cocoa app where I have TextField in status icon's men开发者_如何学Cu, it looks like this:
(source: ifotos.pl)And in couple of places I select its contents programmatically (e.g. in -(BOOL)becomeFirstResponder of my NSTextField subclass)
And it doesn't work. It surely has something to do with the fact that it's in menu, not in window. But how do I fix that?
Because your view is in a menu, it's possible that the textfield isn't responding because the run loop is not in its default mode. Try calling selectText:
like this:
[textField performSelector:@selector(selectText:) withObject:nil
afterDelay:0.0 inModes:[NSArray arrayWithObject:NSRunLoopCommonModes]];
Why don't you just use a window instead? Menus are implemented as windows under the hood: you can do the same thing, just position and style your window appropriately.
Edit: answer largely rewritten
精彩评论