ios - 以编程方式设置在 View Controller 上添加的对象的标签

标签 ios objective-c iphone core-data uitapgesturerecognizer

好吧,这是一个完全令人无法想象的问题。我有一个 View Controller ,在其上添加了 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();
     }
}

我还将点击的位置保存在核心数据中。现在的问题是我无法为 View Controller 上添加的每个自定义 UIView 设置标签。如图所示,每个自定义 UIView 都有一个按钮。单击该 Button 时, View 将导航到另一个 View 。现在我需要为每个 View 设置标签。这怎么可能 ?我不知道我是否能够理清我的概念,但我会发自内心地感激。所以,请给我一个解决方案。我是 iPhone 新手。

最佳答案

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

@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++;

    ...
}

但是我不清楚这个标签值的含义以及您希望如何使用它并跟踪它。如果您需要这方面的帮助,请添加详细信息。

关于ios - 以编程方式设置在 View Controller 上添加的对象的标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25132530/

相关文章:

iphone - 关闭子级后,父级 UIViewController 方向不应更改

objective-c - 检查 iOS 上的 Facebook 权限

ios - 从我的项目中删除外部文件

objective-c - NSScrollView 不适用于 Mountain Lion

ios - IBAction 不让我进入下一页? swift Xcode

iphone - UIApplicationDidFinishLaunchingNotification的使用

ios - UIButton 在第二次单击之前不会以编程方式更新

ios - 处理两个登录按钮

ios - navigationController.viewControllers 在 iOS 8.2 中的工作方式不同

iphone - 阻止 UIAlertViewDelegate