可可:我有我的用户的输入 - 现在什么? [英] Cocoa: I've got my user's input - Now what?

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

问题描述

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



这里是myController.mm,它正在调用并显示窗口来接受用户输入:

  EncryptPasswordDlgController encPassController = [[EncryptPasswordDlgController alloc] init]; 

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

这里是我的nswindowcontroller对象代码:

  #importEncryptPasswordDlgController.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
{
//关闭窗口
password = nil;
[[self window] close];
// return;
}
@end

相应的IBAction方法正在被调用,并按需要执行,即它显示窗口从文本字段获取用户输入,并且点击确定它也更新密码。但是在它完成执行IBAction clickOK方法之后,它应该返回到myController.mm并通过执行下一条语句NSString * inputPassword = [encPassController password];这不是发生。



感谢Bob的答复。
我现在明白我的错误是什么。
所以我试图通过使用while循环的通知方法如下所示:

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

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



没有什么像MFC等效,它直接等待用户输入。
真的很难创建一个窗口并获得用户输入或者我真的在这里缺少一些东西????

解决方案

您的问题是' - [NSWindowController showWindow:]'不阻止等待窗口输入。你调用这个,它告诉你的应用程序显示窗口,但然后它立即执行下一行,设置密码为nil,因为它还没有设置。



IBAction发生在运行循环。基本上,你的应用程序的流程是:



初始化应用程序控制器 - >显示窗口 - >设置密码为nil - >运行循环一次等待输入 - >确定按钮被按下 - >设置控制器密码字段 - >返回到运行循环。



您应该阅读运行循环,正是在这里发生。上面的链接将教你你需要学习什么,还有更多。它不是多线程的,但它不按您期望的顺序运行。基本上,你需要重新排列它,使在myController.mm,你等待输入。这可以通过通知完成,或者仅通过调用IBAction中的一个方法告诉控制器密码已经改变(这本质上是通知做的)。



编辑:实际上,一旦你修复了这一点,你应该确保你调用'保留'你在IBAction期间获得的密码,否则你将崩溃一次字符串自动释放,您尝试访问它。 内存管理指南


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.

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];

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

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.

EDIT:

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;
}

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.

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????

解决方案

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.

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

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.

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).

Edit: 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

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

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