从viewcontroller调用的类方法(NSString类型)返回nil?

发布于 2024-10-12 06:56:24 字数 1084 浏览 1 评论 0原文

我是一名刚接触 Objective-C 的 C++ 程序员。

我创建了一个计算器应用程序,它使用单个视图运行良好。我有一个计算类和一个视图控制器。每次按下按钮时,ViewController 中的 IBAction 方法都会调用 Calculations 类中定义的方法来处理输入,并将输出作为 NSString 返回,然后将其设置为 label.text 字段的值。

现在我正在使用相同的计算类开发标签栏应用程序。该应用程序有两个选项卡,每个选项卡都有一组独特的计算器输入按钮(两个视图共享相同的输入/输出数据)。第一个选项卡与我的第一个具有单一视图的应用程序相同,因此我尝试以类似的方式执行此操作。

问题是:

按下按钮时,处理输入的 IBAction 方法会调用 Calculations 类方法(如下所示),不会出现错误:

-(IBAction)readInput:(id)sender {    
    [_calculations input:[sender titleForState:UIControlStateNormal]];
    inputField.text = [_calculations inputDisplay];
    outputField.text = [_calculations outputDisplay];
}

但是,inputDisplay 和 outputDisplay 方法都返回 nil。使用调试器,我注意到我无法“单步执行”对 _calculations 方法的调用,而是跳过该行,并且两者返回的值均为 nil。我添加了以下方法:

-(IBAction)setNumber:(id)sender {
    NSString *button =(NSString *)[sender titleForState:UIControlStateNormal];
    inputField.text = button;
}

如果我将其附加到输入按钮,我可以看到显示更新。这似乎是调用 _calculations 成员函数和选项卡栏视图的问题(因为使用单个视图不存在此问题)。

我意识到我遗漏了很多信息,但我这样做是为了避免提供不相关的信息。如果需要,我将提供所有必要的详细信息。

I'm a c++ programmer new to objective-c.

I created a calculator app that is working fine using a single view. I have a Calculations class and a ViewController. Every time a button is pressed, an IBAction method in the ViewController calls methods defined in the Calculations class to handle the input and returns the output as an NSString which I then set as the value of the label.text field.

Now I am working on a tab bar app using the same Calculations class. This app has two tabs, each with a unique set of input buttons for the calculator (both views sharing the same input/output data). The first tab is identical to my first app with the single view, so I am trying to do this in a similar fashion.

Here is the problem:

When a button is pressed, the IBAction method that handles the input runs through the calls to the Calculations class methods (shown below) without error:

-(IBAction)readInput:(id)sender {    
    [_calculations input:[sender titleForState:UIControlStateNormal]];
    inputField.text = [_calculations inputDisplay];
    outputField.text = [_calculations outputDisplay];
}

however, both the inputDisplay and outputDisplay methods return nil. Using the debugger I noticed that I am unable to "step into" the calls to _calculations methods, instead the line is skipped and the value returned by both is nil. I added the following method:

-(IBAction)setNumber:(id)sender {
    NSString *button =(NSString *)[sender titleForState:UIControlStateNormal];
    inputField.text = button;
}

and if I attach this to the input buttons I can see the display updated. This seems to be an issue with calling the _calculations member functions and tab bar views (because this issue is not present using a single view).

I realize that I left out a lot of information, but I did it to avoid providing irrelevant information. I will provide all details that are necessary if asked.

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

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

发布评论

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

评论(2

等风也等你 2024-10-19 06:56:24

检查并确保_calculations不为零。

您可以在 nil 上发送任何消息(调用任何方法),它只会返回 nil,不会引发异常。

Check to make sure _calculations is not nil.

You can send any message (call any method) on nil and it will just return nil, not cause an exception.

快乐很简单 2024-10-19 06:56:24

如果没有看到更多代码,诊断将会有点困难。

如果我试图调试这个问题,我首先会确保 _calculations 指向您希望它指向的对象。如果它是从 NIB 加载的,那么它可能不会被初始化,并且仍然为零。您可以毫无问题地向 nil 对象发送消息。如果一个对象接收到一条它无法处理的消息(该方法不存在,或者目标对象为 nil),那么该调用的返回将为 nil。

我过去曾将初始化代码放入 init: 方法中,并花了几个小时为什么它没有被调用,直到我意识到我需要将初始化代码放入 viewDidLoad: 或 initWithNibName:bundle 中: 甚至是 initWithCoder: 选择器。

HTH,马特

Without seeing more code it is going to be a bit difficult to diagnose.

If I was trying to debug this issue I would first make sure _calculations points to the object you want it to point to. If its loaded from a NIB then it might not be getting initialised, and still be nil. You can send messages to nil objects without any issues. If an object receives a message that it cant handle (the method doesn't exist, or the target object is nil) then the return for that call will be nil.

I have in the past put initilization code into the init: method, and spent a few hours why it wasn't being called, until it dawned on me that I needed to put my init code into the viewDidLoad:, or the initWithNibName:bundle: or even the initWithCoder: selector.

HTH, Matt

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