Search code examples
iosmkmapviewuipickerviewmkannotationmkannotationview

Making an app that uses a PickerView to drop pins


Me and a group of friends are creating an iPhone app for a project in our technology class. Our app is going to be used for navigation to different high school sporting event locations in our area. We are currently using a PickerView to control all the combinations of school and sport. The picker has two components, one for schools and one for sports. We already have the MapView set up as well.

We are wondering how we can place the pins for all the different school/sport combinations using the outputs from the Picker View. As you can see in the code below, we currently have a ton of if/then statements to receive the outputs from the PickerView, but eventually want to make that less bulky as well.

Here is the code for the picker outputs:

    - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
int x = [pickerView selectedRowInComponent:0]; NSLog(@"Row 1: %i", x);
int y = [pickerView selectedRowInComponent:1]; NSLog(@"Row 2: %i", y);

//Blue Ridge
if (x == 0 && y == 0)
    {NSLog(@"Blue Ridge Basketball");}

else if (x == 0 && y == 1)
    {NSLog(@"Blue Ridge Basketball");}

else if (x == 0 && y == 2)
    {NSLog(@"Blue Ridge Cross Country");}

else if (x == 0 && y == 3)
    {NSLog(@"Blue Ridge Football");}

else if (x == 0 && y == 4)
    {NSLog(@"Blue Ridge Golf");}

else if (x == 0 && y == 5)
    {NSLog(@"Blue Ridge Soccer");}

else if (x == 0 && y == 6)
    {NSLog(@"Blue Ridge Softball");}

else if (x == 0 && y == 7)
    {NSLog(@"Blue Ridge Track");}

else if (x == 0 && y == 8)
    {NSLog(@"Blue Ridge Volleyball");}

else if (x == 0 && y == 9)
    {NSLog(@"Blue Ridge Wrestling");}

//DeeMack
   else if (x == 1 && y == 0)
    {NSLog(@"DeeMack Baseball");}

   else if (x == 1 && y == 1)
    {NSLog(@"DeeMack Basketball");}

   else if (x == 1 && y == 2)
    {NSLog(@"DeeMack Cross Country");}

   else if (x == 1 && y == 3)
    {NSLog(@"DeeMack Football");}

   else if (x == 1 && y == 4)
    {NSLog(@"DeeMack Golf");}

   else if (x == 1 && y == 5)
    {NSLog(@"DeeMack Soccer");}

   else if (x == 1 && y == 6)
    {NSLog(@"DeeMack Softball");}

   else if (x == 1 && y == 7)
    {NSLog(@"DeeMack Track");}

   else if (x == 1 && y == 8)
    {NSLog(@"DeeMack Volleyball");}

   else if (x == 1 && y == 9)
    {NSLog(@"DeeMack Wrestling");}

}

...etc, This format continues for 11 more schools.

Any ideas as to how we can use these outputs to drop the pins on the map, or ideas to make this code shorter would be greatly appreciated. Also it is important to note that we are all very new to coding, so the simpler the better. Thanks!


Solution

  • To make the code shorter, all you need to do is maintain 2 arrays, one containing the high school names and the other containing the sports. So create 2 NSArrays as properties of your controller

    @property (strong, nonatomic) NSArray *highSchools;
    @property (strong, nonatomic) NSArray *sports;
    

    and then in the viewDidLoad method, do this :

    self.highSchools = [NSArray arrayWithObjects:@"Blue Ridge", @"DeeMack," nil];
    self.sports = [NSArray arrayWithObjects:@"Baseball", @"BasketBall", @"Cross Country", nil];
    

    So now your picker delegate method will reduce to this ::

    - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
        int x = [pickerView selectedRowInComponent:0]; NSLog(@"Row 1: %i", x);
        int y = [pickerView selectedRowInComponent:1]; NSLog(@"Row 2: %i", y);
    
        NSLog(@"%@ %@", [self.highSchools objectAtIndex:x], [self.sports objectAtIndex:y]);
    }
    

    Now, for placing the pins, its not clear from the question, exactly how or where you want to place them and what the pins represent. It seems like you want to drop a single pin on the map depending on a school/sport combination at the location of the selected school. Is that right?