2014-09-02 33 views
0

我有一個帶有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 
+0

void displayFinderAlertWithMessage(NSString * msg)?你最好比這更好。 – 2014-09-02 23:49:29

+0

@ElTomato你是什麼意思? – jamespick 2014-09-03 15:32:19

回答

0

我打電話INT主此功能。

這是你的問題。警報需要完整的應用程序設置並運行其事件循環。爲了打字去它,它將需要是活動的應用程序。

您應該從Xcode的模板創建一個普通的應用程序。從那開始。讓事情在那裏工作。您可以在連接到菜單項或窗口中的按鈕的操作方法中顯示警報。 (我想你也可以在-applicationDidFinishLaunching:應用程序代理方法中提供。)

在你工作之後,如果出於某種原因需要在異常情況下工作(例如命令行工具),那麼你可以在那工作。


您正在修改-drawRect:方法內部的視圖層次結構。你不能這樣做。

首先,-drawRect:可以在視圖的生命多次被調用,你會被添加子視圖每次繪製時間。越來越多的子視圖,一遍又一遍。

其次,即使您小心只在第一次添加它們時,-drawRect:用於繪製,而不是用於更改視圖層次結構。 (如果您需要在繪製之前進行此類更改,則有-viewWillDraw,但我認爲這種情況不適用於此情況)。

您可以改爲將子視圖添加到-initWithFrame:的替代中。

順便說一下,您應該使用NSMakeRect()而不是NSRectFromCGRect(CGRectMake(...))

+0

所以我將代碼添加到應用程序委託,但我仍然有同樣的問題。這一次,當我做任何事情時,我都會看到旋轉的沙灘球。 – jamespick 2014-09-03 15:33:35

+0

這是一個正常的,完整的捆綁應用程序?它是以正常的方式啓動(從Xcode,Finder或Dock)?更新您的問題以顯示新的代碼。你確定Xcode沒有停止過程,因爲它遇到了異常,信號或斷點?您可以使用活動監視器對流程卡住時的樣本進行採樣。把這個樣本報告也放到你的問題中。 – 2014-09-03 15:48:09

+0

該應用程序繼續運行。我更新了我的問題。 – jamespick 2014-09-03 15:51:22

相關問題