2012-05-03 28 views
1

我已經看過所有關於此的其他所有帖子,但我似乎無法得到它。我是iOS和Objective-C的新手,我正在遵循Objective-C和iOS的Big Nerd Ranch指南。我在他們的網站上發現的其中一個挑戰是製作一個簡單的CoreLocation應用程序,並且我試圖將代碼分離成類,以便稍後如果我願意的話可以重用它。使用按鈕調用另一類中的方法iOS

我想讓我的ViewController.m文件中的按鈕調用MyLocation.m文件中的locationManager,這是MyLocation類。我已經看過NSNotificaiton,但我無法讓它正常工作。

我不能爲我的生活弄清楚我失蹤或做錯了什麼。我對這個問題的簡單性表示歉意,我正在儘可能多地學習基礎知識。

下面是我的文件:

ViewController.h:

#import <UIKit/UIKit.h> 
#import "MyLocation.h" 

@interface ViewController : UIViewController 
- (IBAction)GetLocation:(id)sender; 
@property (weak, nonatomic) IBOutlet UILabel *LocationOutput; 

@end 

ViewController.m:

#import "ViewController.h" 
#import "MyLocation.h" 

@interface ViewController() 

@end 

@implementation ViewController 
@synthesize LocationOutput; 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
} 

- (void)viewDidUnload 
{ 
    [self setLocationOutput:nil]; 
    [super viewDidUnload]; 
    // Release any retained subviews of the main view. 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); 
} 

- (IBAction)GetLocation:(id)sender { 

    //WHAT GOES HERE? 
} 
@end 

MyLocation.h:

#import <Foundation/Foundation.h> 
#import <CoreLocation/CoreLocation.h> 

@class MyLocation; 

@protocol MyLocationDelegate <NSObject>; 

@end 

@interface MyLocation : NSObject <CLLocationManagerDelegate> 
@property (nonatomic, strong) CLLocationManager *locationManager; 

@end 

MyLocation.m:

#import "MyLocation.h" 

@implementation MyLocation 
@synthesize locationManager = _locationManager; 

-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation 
{ 
    NSLog(@"Latitude is: %f", newLocation.coordinate.latitude); 
} 

@end 

我知道我沒有輸出任何東西給標籤,但我正在做一個NSLog。

謝謝你的幫助。對此,我真的非常感激。

回答

2

爲了發送消息到一個對象,你需要有一個引用它。在你發佈的兩個課程中,你有同樣的問題:你沒有對他們的引用,所以你將無法發送消息。

ViewController中,您需要以某種方式獲得對MyLocation對象的引用。例如,如果你改變了你的代碼:

- (IBAction)GetLocation:(id)sender { 
    MyLocation *myLocation = [[[MyLocation alloc] init] autorelease]; 
    [myLocation someMethod]; 
} 

這將在每次調用的動作,實例化一個新MyLocation對象,並把它的someMethod消息。就你而言,這有點複雜。你的MyLocation類需要實例化一個CLLocationManager的實例。也許,你需要爲它的一個實例變量,做這樣的事情:

@interface MyLocation { 
    CLLocationManager *_locationMananger; 
} 
@end 

@implementation MyLocation 

- (id)init { 
    self = [super init]; 
    if (self) { 
     _locationManager = [[CLLocationManager alloc] init]; 
     _locationManager.delegate = self; 
    } 
    return self; 
} 

- (void)dealloc { 
    [_locationManager release]; 

    [super dealloc]; 
} 

@end 

然後,你就MyLocation創建的CLLocationManager實例將調用其委託方法時的位置變化。你應該用這種方法做什麼,是在MyLocation的某個實例變量中存儲該位置,以便稍後可以訪問它。

最後,我想改變ViewController只有一次創建MyLocation例如,在它的初始化,或查看負載:

@interface ViewController { 
    MyLocation *_myLocation; 
} 
@end 

@implementation ViewController 

- (id)initWithNibName:(NSString *)nibName bundle:(NSBundle *)bundle { 
    self = [super initWithNibName:nibName bundle:bundle]; 
    if (self) { 
     _myLocation = [[MyLocation alloc] init]; 
    } 
    return self; 
} 

- (void)dealloc { 
    [_myLocation release]; 
    [super dealloc]; 
} 

- (IBAction)GetLocation:(id)sender { 
    // Grab the location from _myLocation using a property or method defined 
} 

@end 

希望你的想法,這可以讓你開始。

+0

非常感謝。這確實使它更清楚。然而,當botton擊中'[myLocation locationManager]'行時,我的程序停止。它轉到MyLocation.m中的@synthesize locationManager = _locationManager;然後什麼也不做。我錯過了什麼?直到今天,我還以爲自己感覺更舒服了。再次感謝你。 – Siriss

+0

請重新檢查示例代碼,我只是做了一個更正:將對象分配給實例變量時,請勿自動釋放它們! – pgb

+0

感謝您的更新和所有的幫助。我沒有autorelease,因爲我正在使用內置的自動參考控制,它仍然卡在同一行。它似乎無法找到locationManager方法 – Siriss