我已经看过大多数关于此事的所有其他帖子,但我似乎无法得到它。我是iOS和Objective-C的新手,我正在关注Objective-C和iOS的Big Nerd Ranch指南。我在他们的网站上发现的挑战之一是制作一个简单的CoreLocation应用程序,我试图将代码分离成类,以便我可以在以后再重复使用它,如果我愿意的话。
我想获取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。
感谢您的帮助。我真的很感激。
答案 0 :(得分: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
然后,您在CLLocationManager
上创建的MyLocation
实例将在位置更改时调用其委托方法。您应该在该方法中执行的操作是将该位置存储在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
希望你能得到这个想法,这可以帮助你开始。