Cocoa: I've got my user's input - Now what?

一个人想着一个人 提交于 2019-12-01 14:22:47

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!