iPhone:我正在尝试使用 if 语句,但它对我不起作用

发布于 2024-07-26 22:43:19 字数 745 浏览 3 评论 0原文

(即将解决,但尚未完全解决) (我的临时解决方案是使用 void 方法并在每个按钮中调用它,这样我就可以编辑多个按钮使用的代码,而不是在进行改进时必须编辑每个单独的按钮 if 语句)

我敢打赌这非常简单我犯了错误,但我找不到它。

我试图在整数达到 50 时显示先前定义的警报; 这是我在 viewDidLoad 方法中使用的 if 语句:

if(count == 50){
    [alert show];
}

当整数达到 50 时,没有任何反应。 我哪里出错了?

整数在 IBAction 中像这样更改:

 count+=10;

如果我删除 if 语句并仅保留警报显示,则它会在视图加载时显示。 所以警报不是问题。

有人指出,如果这段代码只在viewDidLoad方法中运行一次,并且稍后将整数更改为50,那么它不会等待整数为50,而是会看到值不是50而不执行永远保持警惕。

那么我如何用代码观察整数,并在整数为 50 时执行警报, 但不在增加整数的操作中调用警报?

哦,顺便说一句,我尝试在增加整数的操作中调用 if 语句,这很有效,但我有大约 100 个按钮来增加整数计数,并检查每个按钮中的值是否为 50 似乎有点空间所有的复制和粘贴都非常耗时。 另外,我计划在整数达到 50 时不仅仅发出警报。我确信有一种更有效的方法来检查整数计数的值是否为 50。

(Nearly solved, but not quite)
(My temporary solution is to use a void method and call it in every button, and in that way I can edit the code used by multiple buttons rather then when making improvements having to edit each individual buttons if statement)

I bet it's a really simple error i've made, but I can't find it.

I'm trying to show a previously-defined alert when an integer reaches 50; this is the if statement I use in the viewDidLoad method:

if(count == 50){
    [alert show];
}

When the integer reaches 50, nothing happens. Where have I gone wrong?

The integer is changed like this in an IBAction:

 count+=10;

If I remove the if statement and just leave the alert to show, it displays when the view loads. So the alert isn't the problem.

Someone pointed out that if this code is only run once in the viewDidLoad method and the integer is changed to 50 at a later stage it won't wait for the integer to be 50, instead will see the value not being 50 and not execute the alert ever.

So how would I watch the integer with code, and execute an alert when the integer is 50,
But not call the alert in the action that is increasing the integer?

Oh and by the way I have tried calling the if statement in the action that increases the integer and that works but I have about 100 buttons that increase the integer count and checking if the value is 50 in each and every single button seems a bit space consuming and time consuming for all that copy and pasting. Also I plan on having more than just an alert happen when the integer reaches 50. Im sure there's a more efficient way of just checking if the value of the integer count is 50.

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

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

发布评论

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

评论(9

迷荒 2024-08-02 22:43:19

当你说

但是当整数达到 50 时什么也没有发生

这让我相信您在 -viewDidLoad 方法运行之后更改了整数值。 您的 if 语句仅在视图加载时计算一次,因此,如果您在此之后更改整数的值,则需要将警报视图添加到正在更改整数的任何位置。

When you say

But when the integer reaches 50 nothing happens

That leads me to believe that you're changing the integer value after the -viewDidLoad method runs. Your if statement is only evaluated once, when the view loads, so if you're changing the value of the integer after that point you'll need to add the alert view to whatever is changing the integer.

岛歌少女 2024-08-02 22:43:19

尝试

if(count == 50){
    [alert show];}

在计数加 10 后立即在同一个 IBAction 中添加。

Try adding

if(count == 50){
    [alert show];}

In the same IBAction right after you add 10 to count.

一生独一 2024-08-02 22:43:19

这应该没问题——Objective-C 是 C 之上的一个薄层,因此有效的 C 语法应该可以工作。 count 是什么样的变量? 您希望将其定义为 int,而不是 NSNumber。 NSNumber 是一个包装数字的类,因此它可以用作 Objective-C 对象 - 但您不能使用 == 语法来比较它们。

希望有帮助!

This should be fine - Objective-C is a thin layer on top of C, so valid C syntax should work. What kind of variable is count? You want to define it as an int, not an NSNumber. NSNumber is a class that wraps a number so it can be used as an Objective-C object - but you can't compare them using the == syntax.

Hope that helps!

陌路黄昏 2024-08-02 22:43:19

查看密钥-值观察

如果将计数器设置为属性,则应该能够设置一个在值发生变化时触发的观察器。 在该观察者中,您可以进行检查以触发警报。

Check out Key-Value observing

If you make the counter a property, you should be able to setup an observer that fires whenever the value changes. In that observer, you can do your check to fire the alert.

寒冷纷飞旳雪 2024-08-02 22:43:19

尝试

NSLog(@"count is %d",count); 

看看问题是否存在。 很难说 count 是混乱还是警觉,但问题不在于你的 if 语句本身。

Try

NSLog(@"count is %d",count); 

and see if the problem's there. It's hard to say whether count is messed up or alert, but the problem's not with your if statement itself.

音盲 2024-08-02 22:43:19

需要尝试一些事情,

  • 在 if 语句上设置一个断点,将
  • if 语句更改为 count >= 50
  • count 真的是 int 还是 float?

a couple of things to try,

  • set a break point on the if statement
  • change the if statement to count >= 50
  • is count really an int or is it a float?
夜声 2024-08-02 22:43:19

最简单的答案:让所有增加 count 的内容通过控制器中的一个方法来执行此操作,并让该方法检查是否到了显示警报的时间。

更复杂的答案涉及观察值: 键值观察。 您可以设置一个观察者来观察该值,并在达到 50 时响应警报。但这将需要更多代码,并且比仅仅引入一个中央方法来处理该行为更复杂且更容易出现错误。

The simplest answer: Have all the things that increment count go through one method in your controller to do so and have that method check whether it's time to show the alert.

The more complicated answer that involves watching the value: Key-Value Observing. You could set up an observer to watch the value and respond with the alert when it hits 50. But this will take more code and be more complicated and bug-prone than just introducing a central method to handle the behavior.

漫漫岁月 2024-08-02 22:43:19

我不明白,为什么不能在增加计数器的代码中显示警报? 它只需要对 UIAlertView 的引用...或者发布警报持有代码正在侦听的通知。

I don't get it, why can't you show the alert in the code that increments the counter? It jut needs a reference to the UIAlertView... Or post a notification the alert holding code is listening to.

轻拂→两袖风尘 2024-08-02 22:43:19

ViewDidLoad 仅在加载视图时(即创建视图时)运行。 此后,除非特殊情况,否则不会再次运行。

如果您希望它在每次按下按钮时检查计数并增加计数,请在 IBAction 中执行此操作。

您可以创建一个方法来检查计数器,如下所示,

-(BOOL) checkCounter
{
  return count == 50;
}

并在递增计数器后在按钮中调用它:

if ([self checkCounter])
{
//do stuff when it's 50
}

ViewDidLoad is only run when your view is loaded - that is when it is created. After that it won't get run again, barring special circumstances.

If you want it to check the count each time you push a button and increase the count, then do so in the IBAction.

You could make a method that checks the counter like so

-(BOOL) checkCounter
{
  return count == 50;
}

And call it in your buttons like so after you've incremented it:

if ([self checkCounter])
{
//do stuff when it's 50
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文