0


我在 xcode 中有一个 tabbar 项目,在第一个视图中我需要找到我的 GPS 位置,我需要在 appdelegate 上的两个变量上保存经度和纬度。这里有一些代码:

第一视图控制器.h

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

@interface FirstViewController : UIViewController <CLLocationManagerDelegate>{ 
    CLLocationManager *locationManager;     
}    
@property (nonatomic, retain) CLLocationManager *locationManager;
@end

第一视图控制器.m

#import "FirstViewController.h"
#import "CampeggiandoAppDelegate.h"
#import <CoreLocation/CoreLocation.h>

@interface FirstViewController ()

@end

@implementation FirstViewController
@synthesize locationManager;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {

        self.tabBarItem.image = [UIImage imageNamed:@"ic_home2"];
        self.tabBarItem.title=@"Home";
    }
    return self;
}
    - (void)viewDidLoad {

    [super viewDidLoad];
    self.locationManager = [[[CLLocationManager alloc] init] autorelease];
    self.locationManager.delegate = self;

    self.locationManager.distanceFilter=500.0f;
    self.locationManager.desiredAccuracy=kCLLocationAccuracyHundredMeters;
    [self.locationManager startUpdatingLocation];



}

- (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation {

    CampeggiandoAppDelegate *appDelegate=(CampeggiandoAppDelegate*)[[UIApplication sharedApplication]delegate];


    appDelegate.latitudineDel=[NSString stringWithFormat:@"%3.5f", newLocation.coordinate.latitude];
    appDelegate.longitudineDel=[NSString stringWithFormat:@"%3.5f", newLocation.coordinate.longitude];

}

当我运行编译器并出现最合适的视图时,应用程序损坏并出现此异常:

由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[AppDelegate setLatitudineDel:]:无法识别的选择器发送到实例 0x84210b0”

有什么帮助吗?谢谢。

4

1 回答 1

0

在按照您的方式设置变量之前,您的类界面应该如下所示:

@interface CampeggiandoAppDelegate : UIResponder <UIApplicationDelegate> {
// ...
// some ivars
// ...
}

@property (nonatomic, strong) NSString *latitudineDel;
@property (nonatomic, strong) NSString *longitudineDel;

@end

此刻你所拥有的是:

@interface CampeggiandoAppDelegate : UIResponder <UIApplicationDelegate> {
// ...
NSString *latitudineDel;
NSString *longitudineDel

// ...
}

@end

所以这些实例变量没有设置器,这就是引发异常的原因。有关属性的更多信息,请阅读

于 2013-04-26T08:22:54.417 回答