ios - How to do an if statement with a button? -
i'm new objective-c , learning bit bit, , have question it.
i made iphone app incrementing or decreasing number, example, , default number set 0. pressing up, down or restart button have different command options. put play if statement, ex. when label number (0) equals 5 (5) have popup box, or text saying "you have reached number 5"; learn , able implement in future app or game.
viewcontroller.h
#import <uikit/uikit.h> int number; @interface viewcontroller : uiviewcontroller { iboutlet uilabel *count; } - (ibaction)up:(id)sender; - (ibaction)down:(id)sender; - (ibaction)restart:(id)sender; @end
viewcontroller.m
#import "viewcontroller.h" @interface viewcontroller () @end @implementation viewcontroller - (ibaction)up:(id)sender { number = number + 1; count.text = [nsstring stringwithformat:@"%i", number]; } - (ibaction)down:(id)sender { number = number - 1; count.text = [nsstring stringwithformat:@"%i", number]; } - (ibaction)restart:(id)sender { number = 0; count.text = [nsstring stringwithformat:@"%i", number]; } - (void)viewdidload { number = 0; count.text = [nsstring stringwithformat:@"%i", number]; [super viewdidload]; // additional setup after loading view, typically nib. }
you can add method:
- (void)checknumber { if (number == 5) { // show alert } }
and in code:
-(ibaction)up:(id)sender { number = number + 1; count.text = [nsstring stringwithformat:@"%i", number]; [self checknumber]; } -(ibaction)down:(id)sender { number = number - 1; count.text = [nsstring stringwithformat:@"%i", number]; [self checknumber]; }
then, learn naming conventions names wrong (letter capitalisation).
Comments
Post a Comment