开发者

Calculations and display in label

开发者 https://www.devze.com 2023-04-08 21:55 出处:网络
I\'m having a small bit of trouble trying to get this piece of code working. I received some answers related to this code bit but it was solving a different issue. 开发者_如何学Go

I'm having a small bit of trouble trying to get this piece of code working. I received some answers related to this code bit but it was solving a different issue.

开发者_如何学Go

Basically two things for testing purposes that I would like to see working correctly here. One when I try to display dist I would like it to actually get the number that is inputted in the Text Field. Currently when I input something it comes up as zero. (I set it up for doubles cause often a double may be used). The other thing is when I place the timePassed into the display instead of dist it comes up with a serious of numbers that should be the amount of time passed between these two buttons. I'm not sure why but it often displays a negative number and the numbers are a little on the high side to begin with. Any ideas or help is always appreciated. (I'm trying to get these to display on the screen correctly because once I know they are working correctly then I can put them into a calculation and have the calculation display).

-(IBAction)triggerDown:(id)sender{
    [timeStart release];
    timeStart = [[NSDate alloc] init];


}

-(IBAction)triggerUp:(id)sender{
    double calc, dist;
    int mph;
    NSString *display;
    NSTimeInterval timePassed = [timeStart timeIntervalSinceNow]*1000;
    if ([distance.text length]== 0) {
        display = @"Please enter a distance";
    }
    else{
        dist = [distance.text doubleValue];
        display = [[NSString alloc] initWithFormat:@"%d MPH",dist];


    }
    speed.text = display;


    [display release];
}

Just so you understand what is happening. When triggerDown is pressed it is a button that passes the starting time. Then when that same button is released the triggerUp button is called and it is supposed to get the time that passed between the two as well as do calculations. (Calculations haven't been written in yet).


Regarding the negative (and too large by modulus) time: your timeStart is in the past, so when you do [timeStart timeIntervalSinceNow] you get a negative interval. Since timeStart will always be in the past, you can just multiply the interval by -1 to get the positive number. Furthermore, when you multiply the interval a thousand, you turn a second into a thousand seconds - you need to divide instead to get seconds from milliseconds.

As for displaying the 0 in display - try setting the text field's value to the label directly: speed.text = distance.text. Will it work? If not, make sure the text field is connected to the outlet ("distance").

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号