ios - 如何将对象传递给手势识别器选择器

时间:2014-10-21 17:36:04

标签: ios objective-c

在mainViewController上的

,我在UIImageView上显示不同的DOT对象。想象一下它就像标记照片一样。每个DOT都是一个包含ID,title,locationX,locationY等信息的对象。当点击DOT时,它会显示一个InfoViewController,我想显示DOT对象信息。我应该怎么做呢?

这里有一些关于如何为每个DOT对象创建手势识别器的片段:

-(void)viewDidAppear:(BOOL)animated
{

    NSArray *allDotItems = [[DotStore getInstance] allDotItems];
    for(DotStore *item in allDotItems)
    {
        NSLog(@"ID:%@", item.ID);
        NSLog(@"Title:%@", item.title);
        NSLog(@"LocationX:%f", item.locationX);
        NSLog(@"LocationX:%f", item.locationY);

        // Create the DOT
        CGRect viewFrame = CGRectMake(item.locationX, item.locationY, DRAW_RECT_WIDTH, DRAW_RECT_HEIGHT);
        self.abstractDotView = [[DOTAbstractDotView alloc] initDot:viewFrame];
        [[self imageView] addSubview:self.abstractDotView];

        // Register gesture reconginzer for tap within the Dot
        UITapGestureRecognizer *singleTapOnDot =
        [[UITapGestureRecognizer alloc] initWithTarget:self
                                                action:@selector(handleSingleTapWithinDot:)];
        [self.abstractDotView addGestureRecognizer:singleTapOnDot];
    }
}


-(void)handleSingleTapWithinDot:(UITapGestureRecognizer *)recognizer
{
    NSLog(@"in handleSingleTap");

    InfoViewController *view = [[InfoViewController alloc] init];
    view.delegate = self;

    /**I would need to pass DOT object info to InfoViewController.**/

    // Open popup view controller is it isn't already presented by the main controller
    BOOL modalPresent = (BOOL)(self.presentedViewController);
    if (!modalPresent)
    {
        view.modalPresentationStyle = UIModalPresentationFormSheet;
        view.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
        [self presentViewController:view animated:YES completion:nil];
    }
}

1 个答案:

答案 0 :(得分:2)

-(void)handleSingleTapWithinDot:(UITapGestureRecognizer *)recognizer
{
    // This assumes that this tap handler is only used for DOTAbstractDotView objects.
    // Otherwise you need to do a type check.
    DOTAbstractDotView *dotView = (DOTAbstractDotView *)recognizer.view;
    // ...