以编程方式设置在视图控制器上添加的对象标记

时间:2014-08-05 06:35:18

标签: ios objective-c iphone core-data uitapgesturerecognizer

好的,这是一个完全没有想到的问题。我有一个视图控制器,我已添加UITapGestureRecognizer。当用户点击View时,会在该抽头位置添加自定义UIView。以下是UITapGestureRecogniser的代码:

 -(IBAction)singleTap_Detected:(UITapGestureRecognizer *)recognizer
 {
     CGPoint tapPoint = [recognizer locationInView:self.view];
     customView=[CustomPointUIView addCustomView];

     [customView.pointSelectionButton addTarget:self action:@selector(addInfoAction:) forControlEvents:UIControlEventTouchUpInside];

     [self.view addSubview:customView];
     customView.layer.position=CGPointMake(tapPoint.x,tapPoint.y);

     [customViewsArray addObject:[NSValue valueWithCGPoint:tapPoint]];

     NSManagedObjectContext *context = [self.circuit managedObjectContext];

     // if there isn't an Point object, create and configure one
     self.point = [NSEntityDescription insertNewObjectForEntityForName:@"PointDetail"
                                                                inManagedObjectContext:context];
     [self.circuit addPointObject:self.point];

     NSString *pointPosition=NSStringFromCGPoint(tapPoint);

     self.point.position=pointPosition;

     NSError *error = nil;
     if (![context save:&error])
     {
         NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
         abort();
     }
}

我也在Core Data保存了分享的位置。现在的问题是我无法为View控制器上添加的每个自定义UIView设置标记。如图所示,每个自定义UIView都有一个按钮。单击Button,视图将导航到另一个视图。现在我需要为每个视图设置标签。这怎么可能 ?我不知道我能否清除我的概念但是我会深深感激。所以,请给我一个解决方案。我是iPhone新手。

1 个答案:

答案 0 :(得分:2)

您需要将下一个标记值存储为此类的实例变量:

@interface YourClass ()
{
    NSInteger _nextTagValue;
}

确保它已在viewDidLoad(或等效的地方)初始化为您的起始值:

- (void)viewDidLoad
{
    _nextTagValue = 100;
    ...
}

并像这样使用它:

-(IBAction)singleTap_Detected:(UITapGestureRecognizer *)recognizer
{
    CGPoint tapPoint = [recognizer locationInView:self.view];
    customView=[CustomPointUIView addCustomView];
    customView.tag = _nextTagValue++;

    ...
}

但是,我不清楚这个标记值意味着什么以及您希望如何使用它并跟踪它。如果您需要帮助,请添加详细信息。