如何远程导入代码? IOS

时间:2011-06-09 18:44:29

标签: iphone ios import xcode4

在我正在制作的应用程序中,我想从另一个类调用一些代码并将其导入视图控制器。我该怎么做?

我试图导入的.h代码:

#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

我试图从

导入的.m代码
@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
{
    [locationManager release];

    [super dealloc];
}

@end

所有这些代码我都试图远程导入视图控制器。

谢谢!

2 个答案:

答案 0 :(得分:0)

您只需要在代码文件中对此类进行tomreference,然后就可以使用它了。如果要访问委托方法,则需要在viewcontroller <。p>的.h文件中实现

答案 1 :(得分:0)

将这两个文件添加到项目中。在要使用“LocationGetter”类的文件中,只需#import头文件。这应该可行 - 假设您已将CoreLocation.framework添加到项目中。

您需要导入头文件,因为在编译时,编译器需要先查看LocationGetter及其方法的声明,然后才能处理它们。但是,在链接时间之前不需要实际定义,因此只需在项目中使用.m即可。