我有一个NSAlert,它有一个包含两个NSTextField的NSView附件视图。我可以把光标放在NSTextField中,但是我不能输入它们。相反,它将输入我在Xcode中输入的最后一行。我正在使用Xcode 6。
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
NSAlert *passRequest = [[NSAlert alloc] init];
[passRequest setMessageText:@"Finder wants to restart. Type your password to allow this."];
[passRequest addButtonWithTitle:@"OK"];
[passRequest addButtonWithTitle:@"Cancel"];
[passRequest setAccessoryView:[InputView inputViewWithUsername:@"James Pickering"]];
NSImage *lockImage = [[NSImage alloc] initWithContentsOfFile:@"LOCK_YOSEMITE.png"];
[passRequest setIcon:lockImage];
[passRequest runModal];
}我确实实现了LSUIElement密钥,但即使在此之前,它也没有正常运行。否则,这是直接的可可应用程序。
下面是我的InputView代码:
#import "InputView.h"
@interface InputView ()
@property (strong, nonatomic) NSString *username;
@end
@implementation InputView
+ (InputView *)inputViewWithUsername:(NSString *)username {
InputView *view = [[self alloc] initWithFrame:NSRectFromCGRect(CGRectMake(0, 0, 321, 52))];
[view setUsername:username];
return view;
}
- (void)drawRect:(NSRect)dirtyRect {
[super drawRect:dirtyRect];
NSTextField *usernameLabel = [[NSTextField alloc] initWithFrame:NSRectFromCGRect(CGRectMake(0, 32, 71, 17))];
[usernameLabel setStringValue:@"Username:"];
[[usernameLabel cell] setBordered:NO];
[[usernameLabel cell] setBezeled:NO];
[usernameLabel setEditable:NO];
[usernameLabel setSelectable:NO];
[usernameLabel setBackgroundColor:[NSColor clearColor]];
[usernameLabel setFont:[NSFont systemFontOfSize:13]];
[self addSubview:usernameLabel];
NSTextField *passwordLabel = [[NSTextField alloc] initWithFrame:NSRectFromCGRect(CGRectMake(2, 2, 69, 17))];
[passwordLabel setStringValue:@"Password:"];
[[passwordLabel cell] setBordered:NO];
[[passwordLabel cell] setBezeled:NO];
[passwordLabel setEditable:NO];
[passwordLabel setSelectable:NO];
[passwordLabel setBackgroundColor:[NSColor clearColor]];
[passwordLabel setFont:[NSFont systemFontOfSize:13]];
[self addSubview:passwordLabel];
NSTextField *usernameInput = [[NSTextField alloc] initWithFrame:NSRectFromCGRect(CGRectMake(77, 30, 206, 22))];
[usernameInput setStringValue:self.username];
[usernameInput setFont:[NSFont systemFontOfSize:13]];
[self addSubview:usernameInput];
NSTextField *passwordInput = [[NSTextField alloc] initWithFrame:NSRectFromCGRect(CGRectMake(77, 0, 206, 22))];
[passwordInput setFont:[NSFont systemFontOfSize:13]];
[self addSubview:passwordInput];
}
@end发布于 2014-09-03 04:19:51
我在int main中调用这个函数。
这是你的问题。警报需要设置完整的应用程序并运行其事件循环。为了输入到它,它需要是主动的应用程序。
您可能应该从Xcode的模板创建一个普通的应用程序。从这个开始。把事情处理好。您可以在连接到菜单项或窗口中的按钮或其他任何东西的动作方法中显示警报。(我想,您也可以在-applicationDidFinishLaunching:应用程序委托方法中显示。)
在完成了这个工作之后,如果您需要在一个不寻常的上下文(例如命令行工具)中使用这个工具,那么您可以使用它。
您正在修改-drawRect:方法中的视图层次结构。你不能这样做。
首先,在视图的生命周期中可能会多次调用-drawRect:,每次它绘制时都会添加子视图。越来越多的子视图,一遍又一遍。
第二,即使您只在第一次添加它们时非常小心,-drawRect:也用于绘图,而不是用于更改视图层次结构。(如果您需要在绘图之前进行这样的更改,可以使用-viewWillDraw,但我认为这也不适合这种情况。)
您可以将子视图添加到-initWithFrame:的覆盖中。
顺便说一句,您可能应该使用NSMakeRect()而不是NSRectFromCGRect(CGRectMake(...))。
https://stackoverflow.com/questions/25633350
复制相似问题