How to import code remotely? IOS
In the app I'm making I want to call to some code from another class and import it into a view controller. How would I do this?
The .h code that I'm trying to import from:
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
@protocol LocationGetterDelegate <NSObject>
@required
- (void) newPhysicalLocation:(CLLocation *)location;
@end
@interface LocationGetter : NSObject <CLLocationManagerDelegate> {
CLLocationManager *locationManager;
id delegate;
}
- (void)startUpdates;
@property (nonatomic, retain) CLLocationManager *locationManager;
@property(nonatomic , retain) id delegate;
@end
The .m code I'm trying to import from
@synthesize locationManager, delegate;
BOOL didUpdate = NO;
- (void)startUpdates
{
NSLog(@"Starting Location Updates");
if (locationManager == nil)
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
// You have some options here, though higher accuracy takes longer to resolve.
locationManager.desiredAccuracy = kCLLocationAccuracyKilometer;
[locationManager startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Your location could not be determined." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
[alert show];
[alert release];
}
// Delegate method from the CLLocationManagerDelegate protocol.
- (void)locationManager:(CLLocationManager *)manage didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
if (didUpdate)
return;
didUpdate = YES;
// Disable future updates to save power.
[locationManager stopUpdatingLocation];
// let our delegate know we're done
[delegate newPhysicalLocation:newLocation];
}
- (void)dealloc
{
[locationManage开发者_如何转开发r release];
[super dealloc];
}
@end
All this code I'm trying to import remotely into a view-controller.
Thanks!
You just need tomreference this class in your code file and you can use it. If you are accessing delegage methods you need to implement in you .h file of viewcontroller
Add both the files to your project. In the file where you want to use the "LocationGetter" class, just #import the header file. That should make it work -- assuming you have added CoreLocation.framework to your project.
You need to import the header file since at compile time, the compiler will needs to see the declaration of LocationGetter and its methods before it can process them. However, the actual definitions are not needed till link time, so just having the .m around in your project is enough.
精彩评论