我已經看過所有關於此的其他所有帖子,但我似乎無法得到它。我是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。
謝謝你的幫助。對此,我真的非常感激。
非常感謝。這確實使它更清楚。然而,當botton擊中'[myLocation locationManager]'行時,我的程序停止。它轉到MyLocation.m中的@synthesize locationManager = _locationManager;然後什麼也不做。我錯過了什麼?直到今天,我還以爲自己感覺更舒服了。再次感謝你。 – Siriss
請重新檢查示例代碼,我只是做了一個更正:將對象分配給實例變量時,請勿自動釋放它們! – pgb
感謝您的更新和所有的幫助。我沒有autorelease,因爲我正在使用內置的自動參考控制,它仍然卡在同一行。它似乎無法找到locationManager方法 – Siriss