我几乎看到了所有其他与此相关的帖子,但是我似乎无法理解。我是iOS和Objective-C的新手,并且正在遵循Objective-C和iOS的Big Nerd Ranch指南。我在他们的网站上发现的挑战之一是要创建一个简单的CoreLocation应用程序,并且我试图将代码分离为类,以便以后可以根据需要重用。

我想在ViewController.m文件中获取按钮以调用MyLocation.m文件(即MyLocation类)中的locationManager。我已经研究过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。

感谢您的任何帮助。对此,我真的非常感激。

最佳答案

为了向对象发送消息,您需要对其进行引用。在要发布的两个类中,您遇到相同的问题:您没有对它们的引用,因此您将无法向他们发送消息。

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

希望您能想到,这可以帮助您入门。

10-08 07:49