在Objective-C中为NSTextFields分配变量值

时间:2022-09-25 08:54:05

I'm a complete newbie to Objective-C, coming from a background of PHP, assorted dialects of BASIC, and a smattering of C/C++. While I feel I've got a fair handle on setting basic windows up in Interface Builder, however, I'm having trouble manipulating the content of text fields, especially calling values from them and sending values to them.

我是Objective-C的新手,来自PHP的背景,BASIC的各种方言,以及一些C / C ++。虽然我觉得我已经在Interface Builder中设置了基本窗口,但是,我在操作文本字段的内容时遇到了麻烦,特别是从它们调用值并向它们发送值。

I'm using a basic Xcode Cocoa application template with no extras, and created a Cocoa class called "controller.h". In Interface Builder I put together a window with a Secure Text field, a basic NSTextField under it, and a Push Button to submit the form. My controller class is like so:

我正在使用基本的Xcode Cocoa应用程序模板,没有额外的东西,并创建了一个名为“controller.h”的Cocoa类。在Interface Builder中,我将一个带有安全文本字段的窗口,一个基本的NSTextField和一个用于提交表单的按钮组合在一起。我的控制器类是这样的:

//controller.h
#import <Cocoa/Cocoa.h>

@interface controller {
    IBOutlet id userInput;
    IBOutlet id userOutput;
}
- (IBAction)submitButton:(id)sender;
@end

//controller.m
#import "controller.h"

@implementation controller
- (IBAction)submitButton:(id)sender {
    NSBeep();
    [userOutput setStringValue: userInput];
    [userInput setStringValue:nil];
}
@end

I have userInput linked up to the Secure Text field, and userOutput linked to the NSTextField. submitButton is linked to the Push Button. Based on my understanding of how things work, this should assign the value of the secure text in userInput to userOutput, thereby updating what is displayed in the NSTextField, and then reset the field. The beep is there to let me know if the function is being called.

我将userInput链接到Secure Text字段,userOutput链接到NSTextField。 submitButton链接到按钮。根据我对工作原理的理解,这应该将userInput中的安全文本的值分配给userOutput,从而更新NSTextField中显示的内容,然后重置该字段。如果正在调用该函数,则会发出蜂鸣声让我知道。

The beep isn't going off, however, which means that nothing is happening. Interface Builder built the files though, so I know that they've been defined properly and are connected to the proper places. How do I fix it so that the IBAction executes and the values are sent where they need to be?

然而,哔哔声并没有消失,这意味着什么也没发生。 Interface Builder虽然构建了文件,但我知道它们已被正确定义并连接到适当的位置。如何修复它以便IBAction执行并将值发送到需要的位置?

1 个解决方案

#1


1  

A more effective way to see if it's being called is to set a breakpoint (double click in the gutter to the left of the editor) and debug the app instead. The first suggestion is always to sanity-check your connections.

查看是否被调用的更有效方法是设置断点(在编辑器左侧的装订线中双击)并调试应用程序。第一个建议总是要理智 - 检查你的联系。

In addition, you'll probably want to use this line instead:

此外,您可能希望使用此行代替:

[userOutput setStringValue:[userInput stringValue]];

(Note that -stringValue is inherited from NSControl, just like -setStringValue: is.)

(注意-stringValue是从NSControl继承的,就像-setStringValue:is。)


EDIT: It might help to make sure you're actually instantiating the controller object. You can add an -awakeFromNib method to the controller and put a breakpoint in it to see whether this is the case. (See http://www.cocoadev.com/index.pl?AwakeFromNib) If that method isn't getting called, then the connections specified in IB won't be made, either.

编辑:它可能有助于确保您实际实例化控制器对象。您可以将-awakeFromNib方法添加到控制器并在其中放置断点以查看是否是这种情况。 (请参阅http://www.cocoadev.com/index.pl?AwakeFromNib)如果未调用该方法,则也不会进行IB中指定的连接。

#1


1  

A more effective way to see if it's being called is to set a breakpoint (double click in the gutter to the left of the editor) and debug the app instead. The first suggestion is always to sanity-check your connections.

查看是否被调用的更有效方法是设置断点(在编辑器左侧的装订线中双击)并调试应用程序。第一个建议总是要理智 - 检查你的联系。

In addition, you'll probably want to use this line instead:

此外,您可能希望使用此行代替:

[userOutput setStringValue:[userInput stringValue]];

(Note that -stringValue is inherited from NSControl, just like -setStringValue: is.)

(注意-stringValue是从NSControl继承的,就像-setStringValue:is。)


EDIT: It might help to make sure you're actually instantiating the controller object. You can add an -awakeFromNib method to the controller and put a breakpoint in it to see whether this is the case. (See http://www.cocoadev.com/index.pl?AwakeFromNib) If that method isn't getting called, then the connections specified in IB won't be made, either.

编辑:它可能有助于确保您实际实例化控制器对象。您可以将-awakeFromNib方法添加到控制器并在其中放置断点以查看是否是这种情况。 (请参阅http://www.cocoadev.com/index.pl?AwakeFromNib)如果未调用该方法,则也不会进行IB中指定的连接。