I am trying to fill my tableView with a points and distances to current location.
I've got a problem with initialize property.
In .h file:
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
@interface SIBViewController : UIViewController
<CLLocationManagerDelegate>
{
NSArray *_data;
CLLocationManager *locationManager;
CLLocation *currentLocation;
}
@property (nonatomic, retain) CLLocation *currentLocation;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@end
In .m file:
#import "SIBViewController.h"
#import "atmCell.h"
#import "sibAtmData.h"
@interface SIBViewController ()
@end
@implementation SIBViewController
@synthesize currentLocation;
- (void)viewDidLoad
{
[super viewDidLoad];
[self startSignificantChangeUpdates];
_data = [sibAtmData fetchData];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [_data count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"atmCell";
atmCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
sibAtmData *item = [_data objectAtIndex:indexPath.row];
cell.titleLabel.text = item.title;
cell.subtitleLabel.text = item.subtitle;
CLLocationDistance distance = [self.currentLocation distanceFromLocation:item.location];
cell.distanceLabel.text = [NSString stringWithFormat:@"%.1f km", distance/1000];
NSLog(@"distance: %f", distance);
return cell;
}
- (void)startSignificantChangeUpdates
{
// Create the location manager if this object does not
// already have one.
if (nil == locationManager)
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
[locationManager startMonitoringSignificantLocationChanges];
}
// Delegate method from the CLLocationManagerDelegate protocol.
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
// If it's a relatively recent event, turn off updates to save power
self.currentLocation = [locations lastObject];
NSDate* eventDate = currentLocation.timestamp;
NSTimeInterval howRecent = [eventDate timeIntervalSinceNow];
if (abs(howRecent) < 15.0) {
[self.tableView reloadData];
// If the event is recent, do something with it.
NSLog(@"latitude %+.6f, longitude %+.6f\n",
currentLocation.coordinate.latitude,
currentLocation.coordinate.longitude);
}
}
@end
But currentLocation
is empty:
currentLocation CLLocation * 0x00000000
I've tried to write in viewDidLoad
:
currentLocation = [[CLLocation alloc] init];
but this didn't help me.
Memory allocates for object, but object creates without _latitude and _longitude properties What am I doing wrong?