开发者

Couple Obj C issues including an error

So basically in this code I placed below there is a couple things I would like to accomplish. 1) In that first one I would like开发者_如何学编程 the Text Field to close after done is pressed, but it is not doing so. (Found that code on other forums). 2)On that first button when it is pressed down I would like it to send a time to be saved for when the button is released. 3) On release of that button I would like it to calculate the time between the two times along with some other calculations later.

Problem that I am mainly getting here is the error from the NSTimeInterval. It keeps telling me that NSTimeInterval is incompatiable with type NSTimeInterval. So a double is incompatible with a double??? (I have also tried it with NSTimeInterval *timePassed in the .h and just trying to set timePassed = to it and it doesn't work either. Similar error happens.

#import "MphViewController.h"

@implementation MphViewController
@synthesize speed, distance;

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}

-(IBAction)triggerDown:(id)sender{

    timeStart = [NSDate date];

}

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


    [display release];
}

Also if you have a chance to glance at the else statement. I'm not sure if that will work (seeing is after I enter a distance I still cant get rid of the numberpad). Id like to pass mph once it is calculated into that to be displayed on the screen. Very confused with how parsing is done on obj c. Anyways thanks for any and all help.

As for the textField part. My textField that it is assigned to is distance. Should that Bool be written differently to have distance in it or is that just the way it is supposed to be written?


You are denoting primitive types as pointers where they do not need to be. Remove the star (*) from the NSInteger and NSTimerIntervaldeclaration. The if check looks fine but instead of atoi just use:

dist = [distance.text integerValue];

Edit: Notice a couple of memory management issues.

1 . Make sure you properly retain/release the date then later release it in dealloc.

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

2 . Your if statement will result in trying to release an NSString literal which likely will not crash but is incorrect memory management. Just use an autoreleased string and remove the [display release];

...
else{
        dist = [distance.text integerValue];
        display = [NSString stringWithFormat:@"%d MPH",dist];
    }
    speed.text = display;
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜