views:

365

answers:

8

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

+1  A: 

Hey Jessica,

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!

Ben Gotow
Okay thanks for that, but count is defined an an int.
Jessica
A: 

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.

John
The alert is fine, I tried it without an if statement and it loaded with the view. Where would your suggested code be implemented?
Jessica
+4  A: 

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.

Jeff Kelley
Yes the integer does change after the viewDidLoad method runs.Is there another way to have an alert execute when the integer reaches 50 that doesn't have to be called by the action changing the integer?
Jessica
A: 

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?
hhafez
Count is an int.
Jessica
I have tried your suggestions and unfortunately none of them worked. :( Oh and i've added more info to my question if that will help you come up with an answer.
Jessica
+1  A: 

Try adding

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

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

Brandon Schlenker
Yes that works, but is there a way to not have to execute the alert in that action, as I have about a hundred buttons that add a value to count, and I would like to think there is a way to watch the value in the viewDidLoad method and execute the alert when the integer is 50 rather than having each individual button check if the value is 50, also that would produce very unkempt code and would involve a lot of time consuming copy and pasting.
Jessica
viewDidLoad is only called once, as soon as the view is loaded. So you can't really do what you would like to. Try creating a new method- (void)showAlert {if(count == 50){ [alert show];}Then add [self showAlert]; in each of your IBActions. That still involves some copy and pasting, but cleans up your code a bit.
Brandon Schlenker
Okay thanks for that, I've set up my code like that and it is definitely more maintainable now! Thanks.
Jessica
A: 

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.

Chuck
Yes it does seem rather complicated, do you know of any tutorials that cover key value observing anywhere? Or would you be able to suggest some code I could use in the viewDidload method? I have looked at your link to the docs, but those are pretty complicated for a newbie.
Jessica
A: 

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.

Kendall Helmstetter Gelner
I can show the alert in the code that increments the counter, it's just that I have a lot of buttons that increment the counter, and It would be nice to have one piece of code checking that integer in the viewDidLoad method rather then having each and every button check if the integer is the value in which triggers the alert.
Jessica
You can have all the button trigger methods just call one central method that increments the counter and fires the alert..
Kendall Helmstetter Gelner
+1  A: 

Check out Key-Value observing:

http://developer.apple.com/documentation/Cocoa/Conceptual/KeyValueObserving/Concepts/Overview.html#//apple_ref/doc/uid/20001837

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.

Nathaniel Martin