如何在 Objective-C 中创建和访问全局变量
How to make and access global variables in Objective-C
我通过搜索栏在地图上放置了一个大头针,我检索了它的坐标,将它们保存在 myLatitude 和 myLongitude double 中。
这是在以下代码片段中完成的:
class1.h
@interface Location : UIViewController <UISearchBarDelegate, MKMapViewDelegate>
@property (nonatomic, readwrite) double myLatitude;
@property (nonatomic, readwrite) double myLongitude;
class1.m
-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
[self.mySearch resignFirstResponder];
//geoloc
CLGeocoder *geocoder = [[CLGeocoder alloc]init];
[geocoder geocodeAddressString:self.mySearch.text completionHandler:^(NSArray *placemarks, NSError *error)
{
//mark location and center
CLPlacemark *placemark = [placemarks objectAtIndex:0];
MKCoordinateRegion region;
CLLocationCoordinate2D newLocation = [placemark.location coordinate];
region.center = [(CLCircularRegion *)placemark.region center];
//drop pin
MKPointAnnotation *annotation = [[MKPointAnnotation alloc]init];
[annotation setCoordinate:newLocation];
[annotation setTitle:self.mySearch.text];
[self.mapView addAnnotation:annotation];
//scroll to search result
MKMapRect mr = [self.mapView visibleMapRect];
MKMapPoint pt = MKMapPointForCoordinate([annotation coordinate]);
mr.origin.x = pt.x - mr.size.width *0.5;
mr.origin.y = pt.y - mr.size.height *0.25;
[self.mapView setVisibleMapRect:mr animated:YES];
myLatitude = newLocation.latitude;
myLongitude = newLocation.longitude;
NSLog(@"lat: %f, long: %f", myLatitude, myLongitude);
}];
}
现在,我想在不同的 class2 中使用这些双打(myLatitude 和 myLongitude),换句话说,我想我需要这两个双打是全局的,我怎样才能使它们成为全局的,然后我该如何调用他们在第 2 类?任何代码将不胜感激!
最简单的方法可能是使用 singleton.
您可以创建一个新的单例 class,然后可以从您应用中的任何地方访问它。因此,您可能会创建 CoordinateManager
或其他内容,然后使用它来处理您希望成为全局的位置坐标。
例如
CoordinateManager.h
@interface CoordinateManager : NSObject
+ (CoordinateManager *)sharedManager;
@property float latitiude;
@property float longitude;
@end
和CoordinateManager.m
@implementation CoordinateManager
@synthesize latitiude;
@synthesize longitude;
static CoordinateManager *manager = nil;
+ (CoordinateManager *)sharedManager
{
if (manager) {
return manager;
}
else {
@synchronized(self) {
if (!manager) {
manager = [[CoordinateManager alloc] init];
}
return manager;
}
}
}
@end
然后您可以设置如下属性:
[[CoordinateManager sharedManager] setLatitude:1.067594];
只要您导入 CoordinateManager.h
.
,就可以从您应用中的任何位置访问它
我通过搜索栏在地图上放置了一个大头针,我检索了它的坐标,将它们保存在 myLatitude 和 myLongitude double 中。 这是在以下代码片段中完成的:
class1.h
@interface Location : UIViewController <UISearchBarDelegate, MKMapViewDelegate>
@property (nonatomic, readwrite) double myLatitude;
@property (nonatomic, readwrite) double myLongitude;
class1.m
-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
[self.mySearch resignFirstResponder];
//geoloc
CLGeocoder *geocoder = [[CLGeocoder alloc]init];
[geocoder geocodeAddressString:self.mySearch.text completionHandler:^(NSArray *placemarks, NSError *error)
{
//mark location and center
CLPlacemark *placemark = [placemarks objectAtIndex:0];
MKCoordinateRegion region;
CLLocationCoordinate2D newLocation = [placemark.location coordinate];
region.center = [(CLCircularRegion *)placemark.region center];
//drop pin
MKPointAnnotation *annotation = [[MKPointAnnotation alloc]init];
[annotation setCoordinate:newLocation];
[annotation setTitle:self.mySearch.text];
[self.mapView addAnnotation:annotation];
//scroll to search result
MKMapRect mr = [self.mapView visibleMapRect];
MKMapPoint pt = MKMapPointForCoordinate([annotation coordinate]);
mr.origin.x = pt.x - mr.size.width *0.5;
mr.origin.y = pt.y - mr.size.height *0.25;
[self.mapView setVisibleMapRect:mr animated:YES];
myLatitude = newLocation.latitude;
myLongitude = newLocation.longitude;
NSLog(@"lat: %f, long: %f", myLatitude, myLongitude);
}];
}
现在,我想在不同的 class2 中使用这些双打(myLatitude 和 myLongitude),换句话说,我想我需要这两个双打是全局的,我怎样才能使它们成为全局的,然后我该如何调用他们在第 2 类?任何代码将不胜感激!
最简单的方法可能是使用 singleton.
您可以创建一个新的单例 class,然后可以从您应用中的任何地方访问它。因此,您可能会创建 CoordinateManager
或其他内容,然后使用它来处理您希望成为全局的位置坐标。
例如
CoordinateManager.h
@interface CoordinateManager : NSObject
+ (CoordinateManager *)sharedManager;
@property float latitiude;
@property float longitude;
@end
和CoordinateManager.m
@implementation CoordinateManager
@synthesize latitiude;
@synthesize longitude;
static CoordinateManager *manager = nil;
+ (CoordinateManager *)sharedManager
{
if (manager) {
return manager;
}
else {
@synchronized(self) {
if (!manager) {
manager = [[CoordinateManager alloc] init];
}
return manager;
}
}
}
@end
然后您可以设置如下属性:
[[CoordinateManager sharedManager] setLatitude:1.067594];
只要您导入 CoordinateManager.h
.