Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xcode/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c NSAlert不使用NSTextField_Objective C_Xcode_Macos_Cocoa_Nsalert - Fatal编程技术网

Objective c NSAlert不使用NSTextField

Objective c NSAlert不使用NSTextField,objective-c,xcode,macos,cocoa,nsalert,Objective C,Xcode,Macos,Cocoa,Nsalert,我有一个NSAlert,带有NSView附件视图,其中包含两个NSTextField。我可以将光标放在NSTextField中,但无法键入它们。相反,它将键入我在Xcode中键入的最后一行。我使用的是Xcode 6 - (void)applicationDidFinishLaunching:(NSNotification *)aNotification { NSAlert *passRequest = [[NSAlert alloc] init]; [passRequest s

我有一个NSAlert,带有NSView附件视图,其中包含两个NSTextField。我可以将光标放在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
我在int main中调用这个函数

这是你的问题。警报需要设置完整的应用程序并运行其事件循环。为了让输入转到它,它需要是活动的应用程序

您可能应该从Xcode的模板创建一个普通的应用程序。从那开始。在那里工作。您可以通过连接到菜单项或窗口中的按钮或任何内容的操作方法来显示警报。(我想,您也可以在
-applicationdFinishLaunching:
应用程序委托方法中显示。)

在你完成这项工作之后,如果你出于某种原因需要它在一个不寻常的环境中工作(例如命令行工具),你可以在上面工作


您正在修改
-drawRect:
方法中的视图层次结构。你不能这样做

首先,
-drawRect:
在视图的生命周期内可能会被多次调用,并且每次绘制时都会添加子视图。越来越多的子视图,一遍又一遍

其次,即使您第一次小心地添加了它们,
-drawRect:
用于绘制,而不是更改视图层次结构。(如果您需要在绘图之前进行此类更改,则会出现
-viewWillDraw
,但我认为这也不适合这种情况。)

您可以将子视图添加到
-initWithFrame:
的覆盖中


顺便说一下,您可能应该使用
NSMakeRect()
而不是
NSRectFromCGRect(CGRectMake(…)

void displayFinderAlertWithMessage(NSString*msg)?你最好做得更好。@ElTomato你是什么意思?所以我将代码添加到应用程序委托中,但我仍然有同样的问题。这一次,当我用它做任何事情的时候,我得到了旋转的沙滩球。这是一个普通的、完整的捆绑应用程序吗?它是以正常方式启动的(从Xcode、Finder还是Dock)?更新您的问题以显示新代码。您确定Xcode没有因为遇到异常、信号或断点而停止进程吗?您可以使用活动监视器在流程卡住时对其进行采样。把这个示例报告也放到你的问题中。在这一点上,我会猜测在你的
InputView
类中发生的挂起或任何事情。一个示例报告将有助于说明这一点,该类的代码也是如此。