2011-09-30 3 views
0

this 튜토리얼의 소스 코드를 다운로드하고 조금만했습니다. 모두 정상적으로 작동합니다.위치 및 제목이 작동하지 않습니다.

이제 위치와 제목을 구현하는 자체 프로젝트를 만들고 싶습니다. 그러나 모두 작성한 후에는 효과가 없습니다. 나는 정상에 GPS 표시가 보이지 않는다.

나는 다른 응용 프로그램을 실행할 때 그 알았어. Xcode는 오류나 경고를 제공하지 않습니다.

내 코드 : (AppDelegate에서 아무 것도 변경하지 않음).

LocationAndHeadingTestViewController.h

#import <UIKit/UIKit.h> 
#import "LocationController.h" 

@interface LocationAndHeadingTestViewController : UIViewController <LocationControllerDelegate> 
{ 
    LocationController *_locationController; 

    IBOutlet UILabel *_errorLabel; 

    //Location 
    IBOutlet UILabel *_locationTimeLabel; 
    IBOutlet UILabel *_latitudeLabel; 
    IBOutlet UILabel *_longitudeLabel; 
    IBOutlet UILabel *_altitudeLabel; 

    //Heading 
    IBOutlet UILabel *_headingTimeLabel; 
    IBOutlet UILabel *_trueHeadingLabel; 
    IBOutlet UILabel *_magneticHeadingLabel; 
    IBOutlet UILabel *_headingAccuracyLabel; 

    IBOutlet UIImageView *_compass; 
} 

@property (nonatomic, retain) LocationController *locationController; 

@end 

LocationAndHeadingTestViewController.m

#import "LocationAndHeadingTestViewController.h" 

@implementation LocationAndHeadingTestViewController 

@synthesize locationController = _locationController; 

#pragma mark - View lifecycle 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    _locationController = [LocationController new]; 
    _locationController.delegate = self; 
    [_locationController.locationManager startUpdatingLocation]; 
    [_locationController.locationManager startUpdatingHeading]; 
} 


- (void)locationUpdate:(CLLocation *)location 
{ 
    [_locationTimeLabel setText:[NSString stringWithFormat:@"%@", location.timestamp]]; 
    [_latitudeLabel setText:[NSString stringWithFormat:@"%f", location.coordinate.latitude]]; 
    [_longitudeLabel setText:[NSString stringWithFormat:@"%f", location.coordinate.longitude]]; 
    [_altitudeLabel setText:[NSString stringWithFormat:@"%f", [location altitude]]]; 
} 

- (void)headingUpdate:(CLHeading *)heading 
{ 
    [_headingTimeLabel setText:[NSString stringWithFormat:@"%@", heading.timestamp]]; 
    [_trueHeadingLabel setText:[NSString stringWithFormat:@"%f", heading.trueHeading]]; 
    [_magneticHeadingLabel setText:[NSString stringWithFormat:@"%f", heading.magneticHeading]]; 
    [_headingAccuracyLabel setText:[NSString stringWithFormat:@"%f", heading.headingAccuracy]]; 
} 

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

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    // Return YES for supported orientations 
    return (interfaceOrientation == UIInterfaceOrientationPortrait); 
} 

- (void)viewDidUnload 
{ 
    [super viewDidUnload]; 
} 

- (void)dealloc 
{ 
    [_locationController release]; 
    [super dealloc]; 
} 

@end 

LocationController.h

#import <CoreLocation/CoreLocation.h> 

@protocol LocationControllerDelegate 
@required 

- (void)locationUpdate:(CLLocation *)location; 
- (void)headingUpdate:(CLHeading *)heading; 
- (void)locationError:(NSError *)error; 

@end 

@interface LocationController : NSObject <CLLocationManagerDelegate> 
{ 
    CLLocationManager *_locationManager; 
    id _delegate; 
} 

@property (nonatomic, retain) CLLocationManager *locationManager; 
@property (nonatomic, assign) id delegate; 

@end 

LocationController.m

상당히 간단하지만 나는 정말로 뭔가를 간과한다고 생각합니다.

ps. A는 속성을 "유지"의

_locationManager = [[CLLocationManager new] autorelease]; 

을 : 나는 ...> 당신은 CLLocationManager 객체를 autoreleasing하지 않아야

답변

1

위임 ID <에 ID 대리자를 변경합니다. autorelease를 제거해야합니다. dealloc에서 해제 할 것입니다. 한 번만 출시 할 수 있습니다.

세터를 사용하여 설정 한 경우 세터가 대신 사용합니다. 좋아요 :

self.locationManager = [[CLLocationManager new] autorelease]; 
+0

좋은 설명이 효과가있었습니다. 정말 고마워. – Justin

0

iPhone 서비스에서 위치 서비스를 사용할 수 있습니까?

위치 서비스 (전역 액세스)는 가능하지만 위치 서비스 설정의 특정 앱 또한 동시에 위치 액세스를 거부 당할 수 있습니다.

관련 문제