Question

(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.

Was it helpful?

Solution

Try adding

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

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

OTHER TIPS

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.

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!

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.

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.

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?

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.

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.

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
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top