views:

24

answers:

1

I am new to iPhone apps and objective c and I cannot figure out what is wrong with my method the general idea is that it goes to a plist which contain latitudes and longitudes, takes the data stored in the array that the user enters in a text field, and then outputs a pin at that location. Any suggestions would be greatly appreciated. I have only posted the code for the method, but if having any other part of the application would help in debugging my code then I'd be happy to post it.

-(IBAction) showBus {

[busNum resignFirstResponder];

bus *currentBus = [[bus alloc] init];


NSString *txtFieldData = [[NSString alloc] initWithString: busNum.text];



//get data from plist
NSString *myFile = [[NSBundle mainBundle] pathForResource:@"busLocations" ofType:@"plist"];
NSDictionary *dictionary = [[NSDictionary alloc] initWithContentsOfFile: myFile];

[myFile release];

[currentBus setLat:[[dictionary objectForKey:txtFieldData] objectAtIndex:0]];
[currentBus setLong:[[dictionary objectForKey:txtFieldData] objectAtIndex:1]];
//[currentBus setName:[[dictionary objectForKey:txtFieldData] objectAtIndex:2]];

[txtFieldData release];
[dictionary release];

int latit = [[currentBus Lat] intValue];
int longit = [[currentBus Long] intValue];
NSLog(@"latitude is %d and longitude is %d",latit,longit);

/*
[mapView setMapType:MKMapTypeStandard];
[mapView setZoomEnabled:YES];
[mapView setScrollEnabled:YES];
*/

MKCoordinateRegion region = { {0.0, 0.0 }, { 0.0, 0.0 } };
region.center.latitude = latit;
region.center.longitude = longit;


region.span.longitudeDelta = 0.01f;
region.span.latitudeDelta = 0.01f;

[mapView setRegion:region animated:YES]; 
[mapView setDelegate:self];


DisplayMap *ann = [[DisplayMap alloc] init];

ann.title = @"Bus %@",[currentBus name];
ann.coordinate = region.center; 

[mapView addAnnotation:ann];
//[ann release];



[currentBus release];

}

Thanks for the help

+1  A: 

You shouldn't be releasing myFile (you did not allocate it), and you should be releasing ann (you did allocate that).

Joseph Tura
Thanks joseph that fixed one of the problems I also was releasing some other objects too early. The last issue is that when i uncomment this line: //[currentBus setName:[[dictionary objectForKey:txtFieldData] objectAtIndex:2]]; i get an uncaught exception. Any ideas why?
bubster
Maybe there is nothing at index 2?
Joseph Tura