1

我对一个UIView类进行了子类化,并在一个循环中创建了该类的多个实例(每次递增),但是当我尝试设置视图的标签并在它们全部创建后将其记录到控制台时,它们都有一个标签为 1,无论我将标签设置为什么。

任何帮助将不胜感激,谢谢!

我创建子视图的代码在这里:

//for() loop above with i as counter
FlashCardView *subview = [[FlashCardView alloc] initWithFrame:frame];
subview.delegate = self;
subview.viewNum=i+10; //My attempt at a workaround but I cannot get the view with this later so it is not very helpful
[subview setTag:i+10]; //Tried this and subview.tag=i+10;
NSLog(@"%d", subview.tag); //Prints correctly
//Gets added to parent later

NSLog会记录正确的标签,但是当我在UIView子类中记录标签时,它总是将其标签返回为1. 另外,如果我在稍后调用的方法(在viewcontroller)中打印父级的所有子视图,它们都具有标签 1。

4

2 回答 2

3

我不能告诉你为什么,但我可以告诉你如何找到问题所在。在您的 FlashCardView 子类中,添加此方法:

- (void)setTag:(NSInteger)theTag
{
  assert(theTag != 1);
  [super setTag:theTag];
}

然后,当将标签设置为 1 时,断言将触发,您可以查看堆栈跟踪并查看它的来源。

或者,删除断言,并在超级消息上放置断点。

PS:确保启用例外!

于 2013-04-05T13:31:16.007 回答
0

嗨,感谢您的帮助,我发现当我打算输入其他内容时,我不小心输入了子类的变量,将标签设置为 1。@Sunny,感谢您告诉我仔细检查。

于 2013-04-05T13:35:05.880 回答