Cocoa:从单独的类获取方法的返回值

发布于 2024-11-28 03:42:56 字数 889 浏览 1 评论 0原文

我知道这很简单,这就是为什么我一直疯狂地试图自己弄清楚,但我已经束手无策了。

当用户单击按钮时,操作应该执行此操作:

- (IBAction)startAction:(id)sender {
MyClass *anInstance = [[MyClass alloc] init];
NSLog(@"the name is:%@", [anInstance name]);

如您所见,MyClass 有“name”方法,如下所示:

- (NSString *)name {
return [nameInput stringValue];
NSLog(@"stringValue = %@", [nameInput stringValue]);

nameInput 是用户填写名称的文本字段,按下按钮调用“startAction”,该方法应该调用另一个类中的“name”,该类应该从文本字段返回字符串值并生成日志。

实际发生的情况是,当按下开始时,日志显示“名称是:(null)”。第二条日志甚至没有出现。我认为这是因为“名称”方法没有被调用。但我不知道为什么。我应该指定它们位于单独的类中,并且我声明了“名称”方法,如下所示:

- (NSString*)name;

编辑:经过一番闲逛后,我发现当从它自己的类调用“名称”方法时,日志正确显示了在文本字段中。我在“name”方法中添加了一个额外的日志,我可以看到它正在从另一个类调用,但它返回的值仍然是(null),这意味着在阻止类获取文本字段字符串的调用。

编辑:我不确定这是否重要,但其中一个类是 NSObject 的子类,另一个是 NSViewController 的子类。

如果您需要更多信息,请告诉我。 感谢您的帮助。

I know this is simple, which is why I've been trying like mad to figure it out myself but I'm at the end of my rope.

When a user clicks a button an action is supposed to do this:

- (IBAction)startAction:(id)sender {
MyClass *anInstance = [[MyClass alloc] init];
NSLog(@"the name is:%@", [anInstance name]);

As you can see, MyClass has the "name" method, which is this:

- (NSString *)name {
return [nameInput stringValue];
NSLog(@"stringValue = %@", [nameInput stringValue]);

nameInput is a text field which the user fills with a name, presses the button to call "startAction", that method should call "name" in another class, which should return the string value from the text field and make the logs.

What is actually happening is that when start is pressed the log says "the name is:(null)". And the second log doesn't even appear. I assume it's because the "name" method isn't getting called. But I don't know why. I should specify that they are in separate classes and I declared the "name" method like this:

- (NSString*)name;

EDIT: After a bit more fooling around I found that when the "name" method gets called from it's own class, the log correctly shows whatever was in the text field. I put an extra log inside the "name" method, and I can see that it is getting called from the other class, but the value it's returning is still (null) which means something is happening after the call that is preventing the class from getting the text field string.

EDIT: I'm not sure if this is important but one of the classes is a subclass of NSObject, the other is a subclass of NSViewController.

Let me know if you need any more information.
Thanks for the help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不可一世的女人 2024-12-05 03:42:56

name 方法中,您不能在 NSLog() 命令之前返回值——从方法返回是该方法中发生的最后一件事(我'我很惊讶您没有收到有关 NSLog() 函数未调用的警告)。试试这个:

- (NSString *)name {
    NSLog(@"stringValue = %@", [nameInput stringValue]);
    return [nameInput stringValue];
}

至于为什么 nameInput 没有值,我猜你没有在 Interface Builder 中正确连接文本字段。

In the name method, you can't return a value before the NSLog() command -- returning from the method is the last thing that happens in the method (I'm surprised you don't get a warning about the NSLog() function not getting called). Try this instead:

- (NSString *)name {
    NSLog(@"stringValue = %@", [nameInput stringValue]);
    return [nameInput stringValue];
}

As for why nameInput does not have a value, I'm guessing you haven't properly connected the text field correctly in Interface Builder.

锦爱 2024-12-05 03:42:56

我不确定这是否是正确的答案,因为您还没有完全解释 nameInput 来自何处以及它如何获取其值。但我猜测它是 MyClass 的实例变量。在 startAction: 中,您创建一个 MyClass 的新实例。 MyClassinit 方法也初始化 nameInput 吗?由于 alloc 将实例变量初始化为 nil(或 00.0, ... 取决于类型),如果您的 init 方法没有为 nameInput 分配任何其他值,它仍然是nilstartAction:name 发送到新的 MyClass 实例时。在 Objective-C 中向 nil 发送消息在大多数情况下会返回 nil (*),因此将 stringValue 发送到 nameInput > 在本例中返回 nil,因此您会在日志中获得 (null) 输出。

(*) 有关详细信息,请参阅我对问题“发送消息至没有?”

I'm not sure this is the correct answer because you haven't fully explained where nameInput comes from and how it gets its value. But I'm guessing it's an instance variable of MyClass. In startAction: you create a new instance of MyClass. Does the init method of MyClass also initialize nameInput? Since alloc initializes the instance variables to nil (or 0, 0.0, ... depending on the type), and if your init method does not assign any other value to nameInput, it will still be nil at the point when startAction: sends name to the new MyClass instance. Sending a message to nil in Objective-C in most cases returns nil (*), so sending stringValue to nameInput in this case returns nil and hence you get the (null) output in your log.

(*) For more info, see my answer to the question "Sending a message to nil?"

花桑 2024-12-05 03:42:56

- (NSString *)name {
return [nameInput stringValue];
NSLog(@"stringValue = %@", [nameInput stringValue]);

属于

MyClass *anInstance = [[MyClass alloc] init];

其中,我相信 nameInput 不属于其中。

可能会尝试向 name 方法添加一个 textField 参数,并将您的 nameInput 传递给 MyClass,

- (IBAction)startAction:(id)sender {
MyClass *anInstance = [[MyClass alloc] init];
NSLog(@"the name is:%@", [anInstance name:nameInput]);

希望这会有所帮助。

Your

- (NSString *)name {
return [nameInput stringValue];
NSLog(@"stringValue = %@", [nameInput stringValue]);

belongs to

MyClass *anInstance = [[MyClass alloc] init];

To which, I believe, nameInput is not part of it.

May be try adding a textField parameter to the name method and pass your nameInput to MyClass like

- (IBAction)startAction:(id)sender {
MyClass *anInstance = [[MyClass alloc] init];
NSLog(@"the name is:%@", [anInstance name:nameInput]);

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文