2011-06-09 37 views
0

在我正在創建的應用程序中,我想調用另一個類的一些代碼並將其導入到視圖控制器中。我將如何做到這一點?如何遠程導入代碼? IOS

,我試圖導入.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 

我試圖從

@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 

導入所有這些代碼我想要的.M碼遠程導入視圖控制器。

謝謝!

回答

0

你只需要在你的代碼文件中使用tomreference這個類,你就可以使用它。如果您正在訪問取消方法,您需要在您的.h文件中執行viewcontroller

0

將這兩個文件添加到您的項目中。在要使用「LocationGetter」類的文件中,只需#導入頭文件。這應該會使其工作 - 假設您已將CoreLocation.framework添加到您的項目中。

您需要導入頭文件,因爲在編譯時,編譯器需要在處理它們之前看到LocationGetter及其方法的聲明。然而,直到鏈接時間才需要實際的定義,因此只需在項目中使用.m就足夠了。