My app is working just fine right now, but I don't understand why it is. It's a simple app with 1 button and 1 label. When the button is pressed, the label is populated with the users location coordinates.
Here is why I'm confused:
An hour ago, the app was not working properly. My button's action code looked like this in the header:
-(IBAction)gpsButton;
So I thought to myself that maybe I needed to change the action statement to this:
-(IBAction)gpsButton:(id)sender;
I made the change, made the new connection, and changed the action statement in my main file to include the new "(id)sender" as well.
Then, the app was working correctly! It looked like making that change had fixed everything.
However, I just went back and changed the action statement back to the original that was not working for me an hour ago, but for some reason now that is working too!
Keep in mind that both times I made any changes I deleted the app from iPhone, saved the project in xcode, and then ran it fresh on my iPhone.
So my question is, do I need to use A or B?
A:
-(IBAction)gpsButton;
B:
-(IBAction)gpsButton:(id)sender;
Don't get me wrong, I am very happy that my app is running properly now, but I don't understand why it is working for both A and B now, and which one I should actually be using and why. Can someone please clarify this for me and explain the theory behind the correct answer. Thank you.
Here is my ViewController.h code:
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
@interface ViewController : UIViewController <CLLocationManagerDelegate>
@property (nonatomic, strong) IBOutlet UILabel * gpsLabel;
@property (nonatomic, strong) CLLocationManager * gpsLM;
-(IBAction)gpsButton;
@end
And here is my full ViewController.m code:
#import "ViewController.h"
#import <CoreLocation/CoreLocation.h>
@interface ViewController () <CLLocationManagerDelegate>
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations;
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.gpsLM = [[CLLocationManager alloc]init];
[self.gpsLM startUpdatingLocation];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
CLLocation * currentLocation = [locations lastObject];
NSLog(@"%f", currentLocation.coordinate.latitude);
}
-(IBAction)gpsButton{
CLLocation * currentLocation = self.gpsLM.location;
self.gpsLabel.text = [NSString stringWithFormat:@"Your Location is %@", currentLocation];
}
@end