问题描述
我有两节课。 NSViewController是NSViewController的子类,而ViewController是Auf管理ViewController的子类。在Viewcontroller中,我有一个NSTextField,我想成为它的firstResponder,但我没有做到这一点。
I've got two classes. ManagingViewController, a subclass of NSViewController, and ViewController, a subclass auf ManagingViewController. In Viewcontroller I've got a NSTextField which I want to become the firstResponder, but I didn't manage that.
所以它几乎与第29章中的相同Hillegass的书《适用于Mac OS X的可可编程》(下载该书中的示例
So it is nearly the same like the Chapter 29 in Hillegass' book Cocoa Programming for Mac OS X (Download of the book's examples) except of an NSTextField which is set to firstResponder.
有人可以指出我正确的方法吗?
Can anybody point me to the correct way?
推荐答案
您需要使用-[NSWindow makeFirstResponder:]
将文本字段设置为第一响应者。
You need to set the text field as the first responder by using -[NSWindow makeFirstResponder:]
.
由于这是 NSWindow
方法,因此只有在将相应的视图添加到窗口之后(即在已将视图添加为窗口视图层次结构内的子视图。在这本书的示例中,将视图设置为窗口内框的内容视图时,就会发生这种情况。例如:
Since this is an NSWindow
method, it only makes sense after you’ve added the corresponding view to the window, i.e., after you’ve added the view as a subview inside the window view hierarchy. In the book’s example, this happens when you set the view as the content view of the box inside the window. For example:
- (void)displayViewController:(ManagingViewController *vc) {
// Try to end editing
NSWindow *w = [box window];
…
// Put the view in the box
NSView *v = [vc view];
[box setContentView:v];
// Set the first responder
if ([vc class] == [ViewController class]) {
[w makeFirstResponder:[(ViewController *)vc myTextField]];
}
}
这是假定 ViewController
公开了称为 -myTextField
的吸气方法。
This assumes ViewController
exposes a getter method called -myTextField
.
您可以通过以下方法使它更通用视图控制器公开一种方法,该方法返回视图控制器建议作为第一响应者的对象。像这样的东西:
You can make this more generic by having your view controllers expose a method that returns the object that the view controller recommends as the first responder. Something like:
@interface ManagingViewController : NSViewController
…
- (NSResponder *)recommendedFirstResponder;
@end
@implementation ManagingViewController
…
- (NSResponder *)recommendedFirstResponder { return nil; }
@end
然后,在 ManagingViewController的具体子类中
,具有 -recommendedFirstResponder
返回应该是窗口的第一响应者的对象:
And, in your concrete subclasses of ManagingViewController
, have -recommendedFirstResponder
return the object that should be the window’s first responder:
@implementation ViewController
…
- (NSResponder *)recommendedFirstResponder { return myTextField; }
@end
完成此操作后,您可以更改 -displayViewController:
到类似这样的东西:
Having done that, you can change your -displayViewController:
to something like:
- (void)displayViewController:(ManagingViewController *vc) {
// Try to end editing
NSWindow *w = [box window];
…
// Put the view in the box
NSView *v = [vc view];
[box setContentView:v];
// Set the first responder
NSResponder *recommendedResponder = [vc recommendedFirstResponder];
if (recommendedResponder) [w makeFirstResponder:recommendedResponder];
}
这篇关于NSViewController中的firstResponder的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!