图片来自UIPickerView

时间:2016-04-13 13:27:02

标签: ios uipickerview

下午好,

我有一个显示鸟类图片的UIPickerView,在UIPickerView中选择了不同行的不同图片,现在我想点击图片了解有关鸟的更多信息,所以我想到的是当图片显示时弹出的TextView窃听。我将如何在我的代码中实现它?我是编程新手,非常感谢任何帮助。到目前为止这是我的代码。

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
//Load up NSArray object
_fuglar = [[NSArray alloc]initWithObjects:@"Allt árið",@"Hrafn", @"Svartbakur",@"Silfurmáfur",@"Sílamáfur",@"Frá 20.ágúst til 15.mars",@"Grágæs",@"Heiðagæs",@"Frá 1.september til 15.mars",@"Fýll",@"Dílaskarfur",@"Toppskarfur",@"Helsingi",@"Stokkönd", @"Urtönd",@"Rauðhöfðaönd",@"Duggönd",@"Skúfönd",@"Hávella",@"Toppönd",@"Hvítmáfur",@"Hettumáfur",@"Rita",@"Frá 15.apríl til 14.júlí",@"Kjói(í og við friðlýst æðarvarp)",@"Frá 1.september til 25.apríl",@"Álka",@"Langvía",@"Stuttnefja",@"Teista",@"Lundi",@"-Breytilegt-",@"Rjúpa", nil];
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - UIPickerView Methods
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return _fuglar.count;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [_fuglar objectAtIndex:row];
}
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view

{
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 300, 37)];
label.textAlignment = NSTextAlignmentCenter;
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont fontWithName:@"Helvetica" size:22];
if (row == 0 || row == 5 || row == 8 || row == 23 || row == 25 || row == 31) {
    label.textColor = [UIColor redColor];
}else{
    label.textColor = [UIColor blackColor];
}
label.text = [_fuglar objectAtIndex:row];
return label;

if (row == 0) {
    [UIImage imageNamed:@"byssa2"];
} else {
    [_fuglar objectAtIndex:row];
}

}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
//Create UIImage objects



UIImage * byssa2Image = [UIImage imageNamed:@"byssa2.jpg"];
UIImage * hrafnImage = [UIImage imageNamed:@"hrafn.jpg"];
UIImage * svartbakurImage = [UIImage imageNamed:@"Svartbakur.jpg"];
UIImage * silfurmafurImage = [UIImage imageNamed:@"Silfurmafur.jpg"];
UIImage * silamafurImage = [UIImage imageNamed:@"Silamafur.jpg"];
UIImage * gragaesImage = [UIImage imageNamed:@"Gragaes.jpg"];
UIImage * heidargaesImage = [UIImage imageNamed:@"Heidagaes.jpg"];
UIImage * fyllImage = [UIImage imageNamed:@"fyll.jpg"];
UIImage * toppskarfurImage = [UIImage imageNamed:@"Dilaskarfur1.jpg"];
UIImage * dilaskarfurImage = [UIImage imageNamed:@"Toppskarfur.jpg"];
UIImage * helsingiImage = [UIImage imageNamed:@"Helsingi.jpg"];
UIImage * stokkondImage = [UIImage imageNamed:@"Stokkond.jpg"];
UIImage * urtondImage = [UIImage imageNamed:@"Urtond.jpg"];
UIImage * raudhofdaondImage = [UIImage imageNamed:@"Raudhofdaond.jpg"];
UIImage * duggondImage = [UIImage imageNamed:@"Duggond.jpg"];
UIImage * skufondImage = [UIImage imageNamed:@"Skufond.jpg"];
UIImage * havellaImage = [UIImage imageNamed:@"Havella.jpg"];
UIImage * toppondImage = [UIImage imageNamed:@"Toppond.jpg"];
UIImage * hvitmafurImage = [UIImage imageNamed:@"Hvitmafur.jpg"];
UIImage * hettumafurImage = [UIImage imageNamed:@"Hettumafur.jpg"];
UIImage * ritaImage = [UIImage imageNamed:@"Rita.jpg"];
UIImage * alkaImage = [UIImage imageNamed:@"Alka.jpg"];
UIImage * langviaImage = [UIImage imageNamed:@"Langvia1.jpg"];
UIImage * stuttnefjaImage = [UIImage imageNamed:@"Stuttnefja.jpg"];
UIImage * teistaImage = [UIImage imageNamed:@"Teista1.jpg"];
UIImage * lundiImage = [UIImage imageNamed:@"Lundi.jpg"];
UIImage * rjupaImage = [UIImage imageNamed:@"Rjupa1.jpg"];
UIImage * kjoiImage = [UIImage imageNamed:@"Kjoi.jpg"];



switch (row) {
        break;
    case 0:
        fuglarImage.image = byssa2Image;
        break;
    case 1:
        fuglarImage.image = hrafnImage;
        break;
    case 2:
        fuglarImage.image = svartbakurImage;
        break;
    case 3:
        fuglarImage.image = silfurmafurImage;
        break;
    case 4:
        fuglarImage.image = silamafurImage;
        break;
    case 6:
        fuglarImage.image = gragaesImage;
        break;
    case 7:
        fuglarImage.image = heidargaesImage;
        break;
    case 9:
        fuglarImage.image = fyllImage;
        break;
    case 10:
        fuglarImage.image = toppskarfurImage;
        break;
    case 11:
        fuglarImage.image = dilaskarfurImage;
        break;
    case 12:
        fuglarImage.image = helsingiImage;
        break;
    case 13:
        fuglarImage.image = stokkondImage;
        break;
    case 14:
        fuglarImage.image = urtondImage;
        break;
    case 15:
        fuglarImage.image = raudhofdaondImage;
        break;
    case 16:
        fuglarImage.image = duggondImage;
        break;
    case 17:
        fuglarImage.image = skufondImage;
        break;
    case 18:
        fuglarImage.image = havellaImage;
        break;
    case 19:
        fuglarImage.image = toppondImage;
        break;
    case 20:
        fuglarImage.image = hvitmafurImage;
        break;
    case 21:
        fuglarImage.image = hettumafurImage;
        break;
    case 22:
        fuglarImage.image = ritaImage;
        break;
    case 24:
        fuglarImage.image = kjoiImage;
        break;
    case 26:
        fuglarImage.image = alkaImage;
        break;
    case 27:
        fuglarImage.image = langviaImage;
        break;
    case 28:
        fuglarImage.image = stuttnefjaImage;
        break;
    case 29:
        fuglarImage.image = teistaImage;
        break;
    case 30:
        fuglarImage.image = lundiImage;
        break;
    case 32:
        fuglarImage.image = rjupaImage;
        break;

    default:
        break;
}
}

1 个答案:

答案 0 :(得分:2)

首先,我认为你需要的是选择器本身的轻击手势,然后当它被点击时你可以决定该怎么做.. pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component  是不是按你想要的方式工作得太对了?

首先,无论您在何处创建选择器/创建选择器属性

像这样的东西

-(UIPickerView *)pickerView
{
    if (!_pickerView) {

        _pickerView = [[UIPickerView alloc]initWithFrame:CGRectZero];
        _pickerView.dataSource = self;
        _pickerView.delegate = self;

        UITapGestureRecognizer *tapGestureRecogniser = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(pickerTapped:)];
        tapGestureRecogniser.cancelsTouchesInView = NO;
        tapGestureRecogniser.delegate = self;
        [_pickerView addGestureRecognizer:tapGestureRecogniser];
    }

    return _pickerView;
}

在此之后你需要添加一点点以确保我们的点按手势不会干扰我们的拾取器动作,我们需要添加UIGestureRecognizerDelegate这样

@interface MYPickerViewController () <UIPickerViewDataSource, UIPickerViewDelegate, UIGestureRecognizerDelegate>

以及

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {

    return YES;
}

现在已经不在了......我们可以处理点击选择器。我使用CELL_HEIGHT来表示选择器单元格的高度

-(void)pickerTapped:(UITapGestureRecognizer *)tapGestureRecogniser
{
    NSLog(@"pickerTapped");

    CGPoint touchPoint = [tapGestureRecogniser locationInView:[self picker]];

        if (touchPoint.y < ([self picker].frame.size.height + CELL_HEIGHT)/2 && touchPoint.y > ([self picker].frame.size.height/2) - (CELL_HEIGHT/2)) // this makes sure the tap was actually on the selected cell
    {
        NSLog(@"Met tap criteria");

        NSInteger row = [self.pickerView selectedRowInComponent:0];
        [self presentInfoForRow:row];
    }
}

最后,您要求的位置需要弹出警报

-(void)presentInfoForRow:(NSInteger)row
{

     NSString *title = @"";
     NSString *message = @"";

     // change your message depending on the row selected.
     // you used a switch above, but you'd be much better off creating a bird object, putting .image .title and .message fields in and sticking them in an array

    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];

        UIAlertAction *doneAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Done", @"") style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {

        }];

        [alertController addAction:doneAction];

    [self.navigationController presentViewController:alertController animated:YES completion:nil];

}