1

我和一群朋友正在为我们技术课的一个项目创建一个 iPhone 应用程序。我们的应用程序将用于导航到我们地区不同的高中体育赛事地点。我们目前正在使用 PickerView 来控制学校和运动的所有组合。选择器有两个组件,一个用于学校,一个用于运动。我们也已经设置了 MapView。

我们想知道如何使用 Picker View 的输出为所有不同的学校/运动组合放置引脚。正如您在下面的代码中所看到的,我们目前有大量的 if/then 语句来接收来自 PickerView 的输出,但最终也希望减少它的体积。

这是选择器输出的代码:

    - (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");}

}

...等等,这种格式继续适用于另外 11 所学校。

任何关于我们如何使用这些输出在地图上放置大头针的想法,或者使这段代码更短的想法都将不胜感激。同样重要的是要注意,我们都是编码新手,所以越简单越好。谢谢!

4

1 回答 1

0

为了使代码更短,您需要做的就是维护 2 个数组,一个包含高中名称,另一个包含运动。所以创建 2 个 NSArrays 作为控制器的属性

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

然后在viewDidLoad方法中,执行以下操作:

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

所以现在你的选择器委托方法将减少到这个::

- (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]);
}

现在,对于放置引脚,从问题中不清楚,您想要放置它们的确切方式或位置以及引脚代表什么。您似乎想根据所选学校所在位置的学校/运动组合在地图上放置一个图钉。是对的吗?

于 2013-02-13T22:09:41.853 回答