我有一個帶有NSView附件視圖的NSAlert,它包含兩個NSTextField。我可以將光標放在NSTextField中,但我無法輸入它們。相反,它將輸入我在Xcode中輸入的最後一行。我正在使用Xcode 6.NSAlert不能與NSTextField配合使用
- (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
void displayFinderAlertWithMessage(NSString * msg)?你最好比這更好。 – 2014-09-02 23:49:29
@ElTomato你是什麼意思? – jamespick 2014-09-03 15:32:19