从委托类调用方法

时间:2014-03-12 08:47:39

标签: ios objective-c

在我的LocationController.h

// 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方法?

2 个答案:

答案 0 :(得分:0)

假设您要调用

- (void)locationUpdate:(CLLocation*)location;
来自LocationController

方法,您可以使用委托对象。

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
   if (self.delegate != nil)
   {
      CLLocation *location = [locations lastObject];
      [self.delegate locationUpdate:location];
   }
}

答案 1 :(得分:0)

hey didUpdateLocations是CLLocationManager类的委托方法,它为此调用自己,你必须像这样设置CLLocation

@interface MapViewController : UIViewController < CLLocationManagerDelegate> 
相关问题