0

I'm new to Xcode and I'm just trying to create my first app using this tutorial: http://www.appcoda.com/how-to-get-current-location-iphone-user/

All the code seems OK but I'm having trouble actually displaying anything, here is my code

MyLocationViewController.h

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

@interface MyLocationViewController : UIViewController <CLLocationManagerDelegate>
@property (weak, nonatomic) IBOutlet UILabel *latitudeLabel;
@property (weak, nonatomic) IBOutlet UILabel *longitudeLabel;
@property (weak, nonatomic) IBOutlet UILabel *addressLabel;
- (IBAction)getCurrentLocation:(id)sender;
@end

MyLocationViewController.m

#import "MyLocationViewController.h"


@interface MyLocationViewController ()

@end

@implementation MyLocationViewController

CLLocationManager *locationManager;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

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

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)getCurrentLocation:(id)sender {

    locationManager.delegate = self;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;

    [locationManager startUpdatingLocation];


}

#pragma mark - CLLocationManagerDelegate

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
    NSLog(@"didFailWithError: %@", error);
    UIAlertView *errorAlert = [[UIAlertView alloc]
                               initWithTitle:@"Error" message:@"Failed to Get Your Location" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [errorAlert show];
}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    NSLog(@"didUpdateToLocation: %@", newLocation);
    CLLocation *currentLocation = newLocation;

    if (currentLocation != nil) {
        _longitudeLabel.text = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude];
        _latitudeLabel.text = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude];
    }
}
@end

Get anyone help me out it's driving me crazy...

Thanks

Bryan Chen
  • 45,816
  • 18
  • 112
  • 143
WebDevB
  • 492
  • 2
  • 7
  • 25
  • You should tell us what the error is. – Rob Jan 15 '14 at 22:10
  • There is no error... just nothing is shown in the simulator... – WebDevB Jan 15 '14 at 22:12
  • Hook it up to your labels in your xib. There's a few ways to do it. If still having issues, edit question and describe what it's doing. – Stephen J Jan 15 '14 at 22:12
  • I've just noticed this error: '2014-01-15 22:15:07.459 MyLocationDemo[1553:70b] Application windows are expected to have a root view controller at the end of application launch' – WebDevB Jan 15 '14 at 22:17
  • Do i have to add anything in the appdelegate – WebDevB Jan 15 '14 at 23:07
  • @WebDevB No, nothing needs to be added to app delegate. Are you seeing the `NSLog` in your `didUpdateToLocation`? (BTW, only use that method if trying to support iOS versions prior to 6; otherwise use `locationManager:didUpdateLocations:` instead.) If not, check `authorizationStatus` as well as `locationServicesEnabled`. Also, try this on device, not the simulator. But if you are seeing this delegate method called, though, then you should check to see if your `IBOutlet` references are hooked up properly (e.g. log `self.longitudeLabel` and make sure it's not `nil`). – Rob Jan 15 '14 at 23:20
  • Did you have a look at http://stackoverflow.com/questions/7520971/applications-are-expected-to-have-a-root-view-controller-at-the-end-of-applicati – Jake Lin Jan 15 '14 at 23:28
  • Did you solve your problem? Please mark the correct answer to mark this post as answered. – Alex Cio Jan 29 '15 at 13:54

4 Answers4

0

A first guess is that you haven't created the labels in your storyboard, or you've created them but failed to connect them to the right outlets. Try adding a line like:

self.latitudeLabel.text = @"foo";

in your -viewDidLoad method just to confirm that that label is properly connected. You can do the same for the others, too. If "foo" shows up in the label, you know you've connected the labels to the view controller's outlets properly. If it doesn't, you know where to make changes.

Caleb
  • 124,013
  • 19
  • 183
  • 272
0

If by "I'm seeing nothing" you mean a black screen then check to make sure that your main.m file is referencing your App Delegate file by name (spelling and capitalization count). Then check the project settings to make sure that your storyboard's name is selected in the Deployment Info section of the Info section of the project settings (the same screen where you put the icons for the app).

If by "I'm seeing nothing" you mean just the labels you typed in go back to the tutorial and review the "Establish Connections with UI Variables Section" because you missed something there.

Walter
  • 5,867
  • 2
  • 30
  • 43
0

Are you still seeing the error that you mentioned in one of your comments?:

I've just noticed this error: '2014-01-15 22:15:07.459 MyLocationDemo[1553:70b] Application windows are expected to have a root view controller at the end of application launch'

CHECK THE FOLLOWING:

If you are still seeing that error, it sounds like there is something not stung up correctly in your Storyboard. By default, you should see a fairly large arrow pointing to the View Controller that you should be seeing on your Storyboard. Do you see that arrow?

Did you already add all the labels to the View Controller as the tutorial guides you to do? If so, did you connect those labels and other controls to the code?

Connecting IBOutlets

If you see that arrow and are still having problems, I recommend deleting that View Controller and creating it again in case something was disconnected.

Paul Bonneville
  • 1,905
  • 2
  • 13
  • 17
  • Hi Paul, Thanks for the help, unfortunately this didn't work... One question, within the storyboard under 'Custom Class' should I have anything set there? – WebDevB Jan 15 '14 at 23:30
  • The Custom Class for your View Controller should match the filename of your code that controls it. – Walter Jan 15 '14 at 23:41
  • It should say "MyLocationViewController". It should be showing in the drop down box when you click on it... – Paul Bonneville Jan 15 '14 at 23:43
-1

You didn't synthesize your labels yet.

virindh
  • 3,775
  • 3
  • 24
  • 49