2013-08-18 41 views
0

現在已經有這個問題幾天了,似乎無法找到解決方案。這可能是一些非常基本的東西,但仍然不能提出解決方案。所以我試圖讓我的uilabel在汽車或自行車上更新用戶速度。但是當UIlabel更新時,它不會更新正確的值。任何幫助將不勝感激。 繼承人我.h文件中UIlabel不更新用戶當前速度

#import <Foundation/Foundation.h> 
#import <UIKit/UIKit.h> 
#import <MapKit/MapKit.h> 
#import <CoreLocation/CoreLocation.h> 

@interface MainViewController : UIViewController <LoginDelegate,WEPopoverParentView,PopoverControllerDelegate,MainMenuDelegate,MKMapViewDelegate,UIActionSheetDelegate,UIAccelerometerDelegate, CLLocationManagerDelegate, NSObject> 
{ 
    AppDelegate *appDelegate; 
    IBOutlet MKMapView *userMap; 
    CLLocationManager *locationManager; 

} 
@property (strong, nonatomic) IBOutlet UILabel *speedView; 
@property(nonatomic) int speedCount; 
@property (nonatomic,retain) CLLocationManager *locationManager; 
@property (nonatomic, strong) WEPopoverController *popoverController; 

+ (NSString *) speedToMPH: (float) value; 


- (IBAction)btnMenuTapped:(id)sender; 

@end 

和我的.h文件

@implementation MainViewController 
@synthesize speedCount; 
@synthesize speedView; 
@synthesize popoverController; 
@synthesize locationManager; 


- (void)locationError:(NSError *)error { 
    speedView.text = [error description]; 
} 



-(void)viewWillAppear:(BOOL)animated 
{ 
    [userMap setRegion:MKCoordinateRegionMakeWithDistance(userMap.userLocation.coordinate, 5, 5) animated:YES]; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    appDelegate = [[UIApplication sharedApplication] delegate]; 
    locationManager =[[CLLocationManager alloc] init]; 
    // Do any additional setup after loading the view, typically from a nib. 
    [userMap setCenterCoordinate: userMap.userLocation.coordinate animated: YES]; 
    [self performSelector:@selector(checkForLogin) withObject:nil afterDelay:1]; 

    [self startLocationServices]; 
     // create LM 
     self.locationManager = [CLLocationManager new]; 

     // set its delegate 
     [self.locationManager setDelegate:self]; 

     // set configuration 
     [self.locationManager setDesiredAccuracy:kCLLocationAccuracyBest]; 
     [self.locationManager setDistanceFilter:kCLDistanceFilterNone]; 

     // start location services 
     [self.locationManager startUpdatingLocation]; 
    // create an oldLocation variable if one doesn't exist 
    } 


- (void)startLocationServices 
{ 
    // create the Location Manager 
    if (self.locationManager == nil) { 
     self.locationManager = [CLLocationManager new]; 
    } 

    // stop services 
    [self.locationManager stopUpdatingLocation]; 
    [self.locationManager setDelegate:nil]; 
    self.speedView.text = @"Location Services stopped."; 
} 
// locationManager didUpdateToLocation FromLocation 
    - (void)locationManager:(CLLocationManager *)manager 
didUpdateToLocation:(CLLocation *)newLocation 
fromLocation:(CLLocation *)oldLocation { 
    NSLog(@"%@", newLocation); 
     self.speedView.text = [NSString stringWithFormat:@"%d %.f ", speedCount, [newLocation speed]]; 

    } 

+ (NSString *) speedToMPH: (float) value 
{ 
NSString *speedCount = @"0.0 "; 
if (value>0) { 
float mile = 1609.344f; 
float mph = value/mile * 3600; 
speedCount = [NSString stringWithFormat: @"%.f ", mph]; 
} 
return speedCount; 
} 
+0

你怎麼知道這不是正確的值?顯示的價值是多少?您預期看到的價值是多少? –

+0

所以我在開車時在手機上試了一下。所以我會去30英里每小時,myuilabel會說我在17.3開車。任何想法爲什麼 – mrorgon

+0

@mrios你的問題與Xcode或iPhone無關。這就是標籤被刪除的原因。 – rmaddy

回答

1

CLLocation返回米每秒的速度。因此,您必須將其轉換爲每小時英里數乘以2.23694。

self.speedView.text = [NSString stringWithFormat:@"%d %.f ", speedCount, [newLocation speed] * 2.23694]; 
+1

您還應該使用'NSNumberFormatter'來正確格式化數字,以便按照預期的方式向不同地區的用戶顯示。 – rmaddy

+0

非常感謝您的幫助 – mrorgon