Ios 从委托类调用方法
在我的位置控制器Ios 从委托类调用方法,ios,objective-c,Ios,Objective C,在我的位置控制器 // protocol for sending location updates to another view controller @protocol LocationControllerDelegate @required - (void)locationUpdate:(CLLocation*)location; - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray
// protocol for sending location updates to another view controller
@protocol LocationControllerDelegate
@required
- (void)locationUpdate:(CLLocation*)location;
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations;
@end
@interface LocationController : NSObject {
CLLocationManager* locationManager;
CLLocation* location;
__weak id delegate;
}
@property (nonatomic, strong) CLLocationManager* locationManager;
@property (nonatomic, strong) CLLocation* location;
@property (nonatomic, weak) id delegate;
+ (LocationController*)sharedLocationController; // Singleton method
@end
在LocationController.m中
static LocationController* sharedCLDelegate = nil;
@implementation LocationController
@synthesize locationManager, location, delegate;
- (id)init
{
self = [super init];
if (self != nil) {
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
self.locationManager.distanceFilter = 10.0f;
self.locationManager.headingFilter = 5;
[self.locationManager startUpdatingLocation];
}
return self;
}
#pragma mark -
#pragma mark CLLocationManagerDelegate Methods
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
}
- (void)locationManager:(CLLocationManager*)manager
didFailWithError:(NSError*)error
{
}
#pragma mark - Singleton implementation in ARC
+ (LocationController *)sharedLocationController
{
static LocationController *sharedLocationControllerInstance = nil;
static dispatch_once_t predicate;
dispatch_once(&predicate, ^{
sharedLocationControllerInstance = [[self alloc] init];
});
return sharedLocationControllerInstance;
}
+ (id)allocWithZone:(NSZone *)zone {
@synchronized(self) {
if (sharedCLDelegate == nil) {
sharedCLDelegate = [super allocWithZone:zone];
return sharedCLDelegate; // assignment and return on first allocation
}
}
return nil; // on subsequent allocation attempts return nil
}
- (id)copyWithZone:(NSZone *)zone
{
return self;
}
然后,我想在另一个ViewController中调用其中一个方法,因此在我的viewDidLoad中,我设置了委托[LocationController sharedLocationController].delegate=self代码>然后我想从LocationController类调用一个方法
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
//How can I call the CLLocation from the LocationController class so I don't have to create a new one
CLLocation *location = [locations lastObject];
}
另外,如何在viewDidLoad中调用此方法
因此,我的问题是如何重用LocationController类中的CLLocation,以及如何在我的ViewController中实现didUpdateLocations方法?假设您想调用
- (void)locationUpdate:(CLLocation*)location;
方法,您可以使用委托对象
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
if (self.delegate != nil)
{
CLLocation *location = [locations lastObject];
[self.delegate locationUpdate:location];
}
}
heydidUpdateLocations
是CLLocationManager类的委托方法,它为此调用了自身。您必须像这样设置CLLocation
@interface MapViewController : UIViewController < CLLocationManagerDelegate>
@interface MapViewController:UIViewController
委托需要设置为什么?获取未定义错误。@joshuahornby10:self.delegate
您在LocationController
中声明了该委托,对吗?我有[LocationController sharedLocationController]。delegate=self@joshuahornby10:从LocationController,您需要调用视图控制器的LocationControllerDelegate的委托方法,对吗?对不起,我不明白您的意思,您能给出一个代码示例吗?谢谢