0

In my program, I need to get the value of a label as an int. How do I do so?

This is the code:

@implementation ViewController
@synthesize webview;

- (void)viewDidLoad
{
    [super viewDidLoad];
    [webview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"google.com"]]];
    [super viewDidLoad];

    self.motionManager = [[CMMotionManager alloc] init];
    self.motionManager.accelerometerUpdateInterval = 1;

    if ([self.motionManager isAccelerometerAvailable]) {
        NSOperationQueue *queue = [[NSOperationQueue alloc] init];
        [self.motionManager startAccelerometerUpdatesToQueue:queue withHandler:^(CMAccelerometerData *accelerometerData, NSError *error) {
            dispatch_async(dispatch_get_main_queue(), ^{
                self.xAxis.text = [NSString stringWithFormat:@"%.2f",accelerometerData.acceleration.x];
                self.yAxis.text = [NSString stringWithFormat:@"%.2f",accelerometerData.acceleration.y];
                self.zAxis.text = [NSString stringWithFormat:@"%.2f",accelerometerData.acceleration.z];
            });
        }]; 
    } else 
        NSLog(@"not active");
}

@end

How do I get xAxis’s value as an int. After getting its value as an int, I want to compare it in an if statement.

Thanks

Matt Zanchelli
  • 357
  • 1
  • 12

2 Answers2

0

Simply do this, but I prefer NSInteger so that in 64 bit devices, no performance is affected. NSInteger adapts itself depending on which bit processor the device is using.

[self.xAxis.text integerValue];

But if you insist on using ints

[self.xAxis.text intValue];
Nate Lee
  • 2,842
  • 1
  • 24
  • 30
0

In the motion manager updates handler, CMMotionManager will provide a reference to a CMAccelerometerData object. That CMAccelerometerData object has a CMAcceleration property (acceleration). CMAcceleration is struct with x, y, and z components, each defined as a double. It looks like you are already using this to populate some labels’ text values. In this handler block, you already have a number representation of the x values. If you want to perform a check (if statement), this is the value you should be using.

- (void)viewDidLoad
{
    [super viewDidLoad];

    [webview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"google.com"]]];

    self.motionManager = [[CMMotionManager alloc] init];
    self.motionManager.accelerometerUpdateInterval = 1;

    if ([self.motionManager isAccelerometerAvailable]) {
        [self.motionManager startAccelerometerUpdatesToQueue:[NSOperationQueue mainQueue] withHandler:^(CMAccelerometerData *accelerometerData, NSError *error) {
            double x = accelerometerData.acceleration.x;
            double y = accelerometerData.acceleration.y;
            double z = accelerometerData.acceleration.z;

            self.xAxis.text = [NSString stringWithFormat:@"%.2f", x];
            self.yAxis.text = [NSString stringWithFormat:@"%.2f", y];
            self.zAxis.text = [NSString stringWithFormat:@"%.2f", z];

            if (x > 0.5) {
                self.xAxis.textColor = [UIColor redColor];
            } else {
                self.xAxis.textColor = [UIColor blackColor];
            }
        }]; 
    } else {
        NSLog(@"Accelerometer is not available.");
    }
}

This will color the xAxis label red whenever the x value is over 0.5 and return it to black when it’s less than 0.5.

A few other things to note:

• Instead of calling dispatch_async(dispatch_get_main_queue(), … you can just get the accelerometer updates on the main queue directly. For what’s going on here, this is fine to perform entirely on the main thread.

• Only make one call to [super viewDidLoad]. UIViewController or whatever UIViewController subclass you’re subclassing likely does not expect viewDidLoad to be called multiple times, leading to undefined behaviour.

Matt Zanchelli
  • 357
  • 1
  • 12