Program crashes when selector button is pushed


#1

My program runs fine until a selector button is pushed, then it crashes.

Here is my .h file

[code]#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>

@interface WhereamiViewController : UIViewController
<CLLocationManagerDelegate, MKMapViewDelegate, UITextFieldDelegate>

{
CLLocationManager *locationManager;

IBOutlet MKMapView *worldView;
IBOutlet UIActivityIndicatorView *activityIndicator;
IBOutlet UITextField *locationTitleField;
IBOutlet UISegmentedControl *segControl;

}

  • (IBAction)selectSegmentControl;

  • (void)findLocation;

  • (void)foundLocation:(CLLocation *)loc;

  • (void)setMapType;

@end[/code]

and my .m file

[code]#import “WhereamiViewController.h”
#import “BNRMapPoint.h”

@implementation WhereamiViewController

  • (void)findLocation
    {
    [locationManager startUpdatingLocation];
    [activityIndicator startAnimating];
    [locationTitleField setHidden:YES];
    }

  • (void)foundLocation:(CLLocation *)loc
    {
    CLLocationCoordinate2D coord = [loc coordinate];

    // Creat an instance of BNRMapPoint with the current data

    BNRMapPoint *mp = [[BNRMapPoint alloc] initWithCoordinate:coord title:[locationTitleField text]];

    // Add it to the map view

    [worldView addAnnotation:mp];

    // Zoom the region to this location

    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(coord, 250, 250);
    [worldView setRegion:region animated:YES];

    // Reset the UI

    [locationTitleField setText:@""];
    [activityIndicator stopAnimating];
    [locationTitleField setHidden:NO];
    [locationManager stopUpdatingLocation];

}

  • (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if(self) {

      locationManager = [[CLLocationManager alloc] init];
      
      
      [locationManager setDelegate:self];
    
      
      [locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
    

    }
    return self;
    }

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

    NSTimeInterval t = [[newLocation timestamp] timeIntervalSinceNow];

    if (t< -180) {

      return;
    

    }

    [self foundLocation:newLocation];

}

  • (void)locationManager:(CLLocationManager *)manager
    didFailWithError:(NSError *)error
    {
    NSLog(@“Could not find location: %@”, error);
    }

  • (void)dealloc
    {

    [locationManager setDelegate:nil];
    }

  • (void)viewDidLoad

{
[worldView setShowsUserLocation:YES];
[segControl addTarget:self
action:@selector(action)
forControlEvents:UIControlEventValueChanged];

}

  • (IBAction)selectSegmentControl:(id)sender

{
if ([segControl selectedSegmentIndex] == 0) {
[worldView setMapType:MKMapTypeStandard];
}

if ([segControl selectedSegmentIndex] == 1) {
    [worldView setMapType:MKMapTypeSatellite];
}

[worldView setMapType:MKMapTypeHybrid];

}

  • (void)mapView:(MKMapView *)mapView
    didUpdateUserLocation:(MKUserLocation *)userLocation
    {
    CLLocationCoordinate2D loc = [userLocation coordinate];
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(loc, 250, 250);
    [worldView setRegion:region animated:YES];

}

  • (BOOL)textFieldShouldReturn:(UITextField *)textField
    {
    [self findLocation];
    [textField resignFirstResponder];

    return YES;
    }

@end[/code]

I suspect the

[segControl addTarget:self
action:@selector(action)
forControlEvents:UIControlEventValueChanged];

is wrong, I’m not sure if I even need it since I utilized and IBAction method but I think that I do.


#2

Where did you define the method [color=#FF0000]action[/color]?


#3

I have since defined it and the program worked,
Thank you