我在xcode中有一个tabbar项目,在第一个视图中我需要找到我的GPS位置,我需要在appdelegate上保存两个变量的经度和纬度。这里有一些代码:
FirstViewController.h
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
@interface FirstViewController : UIViewController <CLLocationManagerDelegate>{
CLLocationManager *locationManager;
}
@property (nonatomic, retain) CLLocationManager *locationManager;
@end
FirstViewController.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'
有任何帮助吗?感谢。
答案 0 :(得分: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
因此没有这些实例变量的setter,这就是抛出异常的原因。 有关属性的更多信息,请阅读this。