好吧,这是一个完全令人无法想象的问题。我有一个 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/