OGeek|极客世界-中国程序员成长平台

标题: ios - 以编程方式设置在 View Controller 上添加的对象的标记 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 11:00
标题: ios - 以编程方式设置在 View Controller 上添加的对象的标记

好的,这是一个完全不在意的问题。我有一个添加了 UITapGestureRecognizer 的 View Controller 。当用户点击 View 时,自定义 UIView 会添加到该点击位置。这是 UITapGestureRecogniser 的代码:

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

     [customView.pointSelectionButton addTarget:self actionselector(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"ointDetail"
                                                                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 Controller 上添加的每个自定义 UIView 设置标签。如图所示,每个自定义 UIView 都有一个按钮。单击该 Button 时, View 正在导航到另一个 View 。现在我需要为每个 View 设置标签。这怎么可能 ?我不知道我是否能够清除我的概念但我会发自内心的感谢。所以,请给我一个解决方案。我是 iPhone 新手。



Best Answer-推荐答案


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

@interface YourClass ()
{
    NSInteger _nextTagValue;
}

确保它在 viewDidLoad 中初始化为您的起始值(或其他等价物):

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

并像这样使用它:

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

    ...
}

但是我不清楚这个标签值 意味着什么 以及你想如何使用它和跟踪它。如果您需要这方面的帮助,请添加详细信息。

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






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4