I tried to control the num
variable through UIStepper
, but when I pressed the increase button,
It always start count from 0 not 10,
I am curious what did I miss in my code,
Thank you in advance for any comments
#import "ViewController.h"
int num = 10;
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UILabel *myLabel;
- (IBAction)myStepper:(UIStepper *)sender;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
_myLabel.text = [NSString stringWithFormat:@"%i", num];
}
- (IBAction)myStepper:(UIStepper *)sender {
num = [sender value];
_myLabel.text = [NSString stringWithFormat:@"%i", num];
}
@end
You're never setting the value of the stepper to num
. Either set it in Interface Builder's Attributes Inspector, or create an outlet to your stepper:
@property (weak, nonatomic) IBOutlet UIStepper myStepper;
And then in -viewDidLoad
, set it like this:
self.myStepper.value = num
As an aside, I would recommend against using a global variable. I would make a property for num
instead.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments