Cocoa:我得到了用户的输入——现在呢? [英] Cocoa: I've got my user's input - Now what?

查看:16
本文介绍了Cocoa:我得到了用户的输入——现在呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简而言之,我的程序所做的是:它使用 nswindow(由我的 NSWindowController 对象控制)定期执行并接受用户输入并继续执行.

In a nutshell what my program does is: it executes and takes user input periodically using nswindow (which is controlled by my NSWindowController object) and continues execution.

这是我的 myController.mm,它正在调用并显示窗口以获取用户输入:

here is my myController.mm which is calling and showing the window to take user input:

EncryptPasswordDlgController encPassController = [[EncryptPasswordDlgController alloc] init];

[encPassController showWindow:self];
NSString *inputPassword = [encPassController password];

这是我的 nswindowcontroller 对象代码:

here is my nswindowcontroller object code:

#import "EncryptPasswordDlgController.h"

@implementation EncryptPasswordDlgController
-(id) init
{
    return self;
}

- (IBAction)showWindow:(id)sender
{
    [super showWindow:sender];
    encryptPasswordDlgWindowController = [[NSWindowController alloc] initWithWindowNibName:@"EncryptionPasswordDlg"];
    [encryptPasswordDlgWindowController loadWindow];
    [[self window] makeKeyAndOrderFront:[self window]];

    return;
}

-(IBAction)clickOK:(id) sender
{
    password = [passwordField stringValue];

    NSLog(@"password is %@", password);


    [[self window] close];
    return;
}

-(NSString *)password
{   
    return password;
}

-(IBAction)clickCancel:(id) sender
{
// close the window
    password = nil;
    [[self window] close];
    //return;
}
@end

单击确定或取消按钮后,将调用相应的 IBAction 方法并按要求执行,即它显示窗口从文本字段中获取用户输入,单击确定时它也在更新密码.但是在它执行完 IBAction clickOK 方法之后,它应该返回到 myController.mm 并通过执行下一条语句NSString *inputPassword = [encPassController password];"来检索密码这没有发生.请有人提出解决这种情况的方法.

after i click the ok or cancel button, the respective IBAction method is getting called and is getting executed as required i.e it is showing the window taking user input from text field and on clicking ok it is updating the password also. But after it finishes execution of IBAction clickOK method, it should return back to myController.mm and retrieve the password by executing the next statement "NSString *inputPassword = [encPassController password];" which is not happening. Please can anone suggest a way to tackle this situation.

感谢鲍勃的回复.我现在明白我的错误是什么了.所以我通过使用while循环尝试了通知方法,如下所示:

Thanks Bob for the reply. I now understood what my mistake was. So i tried the notification method by using a while loop as shown:

while(isPasswordReceivedForEncryption == NO)
{   
            sleep(10000);
            //continue;
}

在我的程序中间等待通知到来并将 bool 变量设置为 true.但现在的问题是它进入了无限循环,我的应用程序被卡住了.

in the middle of my program so as to wait till the notification comes and sets the bool variable to true. But now the problem is that it is going into infinite loop and my applicaton is getting stuck.

没有什么像 MFC 等价物那样直接等待用户输入.真的很难创建一个窗口并获得用户输入还是我真的在这里错过了什么????

is there nothing like the MFC equivalent where it directly waits for user input. Really is it that difficult to create a window and get user input or am i really missing something over here????

推荐答案

你的问题是 '-[NSWindowController showWindow:]' 没有阻塞等待窗口输入.您调用它,它告诉您的应用程序显示窗口,但随后它立即执行下一行,将密码设置为 nil,因为它尚未设置.

Your problem is that '-[NSWindowController showWindow:]' does not block waiting for window input. You call this, which tells your app to show the window, but then it immediately executes the next line, setting the password to nil, since it hasn't been set yet.

IBAction 发生在 Run Loop 期间.基本上,您的应用程序的流程是:

The IBAction occurs during a Run Loop. Basically, the flow of your app is:

初始化 App Controller -> 显示窗口 -> 将密码设置为 nil -> 多次运行 Run Loop 等待输入 -> 按下确定按钮 -> 设置控制器密码字段 -> 回到运行循环.

Initialize App Controller -> show the window -> set password to nil -> run the Run Loop a bunch of times waiting for input -> Ok button gets pressed -> set the controllers password field -> go back to the run loop.

您应该阅读运行循环以尝试了解这里到底发生了什么.上面的链接将教您需要学习的内容以及更多内容.它不是多线程的,但它没有按照您期望的顺序运行.基本上,您需要重新排列它,以便在 myController.mm 中等待输入.这可以通过 Notifications 来完成,或者只是通过在 IBAction 中调用一个方法,告诉控制器密码已更改(这实际上是 Notification 所做的).

You should read up on run loops to try to understand what exactly is happening here. The above link will teach you what you need to learn, and much more. It's not multithreaded, but it is not running in the order you expect. Basically, you need to rearrange it such that in myController.mm, you wait for the input. This could be done via Notifications, or just by calling a method in the IBAction that tells the controller the password has changed (which is essentially what the Notification does).

实际上,一旦你解决了这个问题,你应该确保在 IBAction 期间获得的密码上调用保留",否则一旦该字符串被自动释放并尝试访问它,你就会崩溃.内存管理指南

Actually, once you fix this, you should be sure that you call 'retain' on the password you get during the IBAction, otherwise you will crash once that string gets autoreleased and you try to access it. Memory Management Guide

这篇关于Cocoa:我得到了用户的输入——现在呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆