【发布时间】:2014-04-03 11:23:22
【问题描述】:
我有一个UIButton,单击它时,将执行以下块。此块创建一个UITextView 并将其作为子视图添加到带有标签 的新UIView。在多次单击UIButton 时,此代码块将执行并输出多个 UIView 一个接一个。
int commentCount=1;
y=0;
- (IBAction)OnClickAddAnother:(id)sender
{
UIView *_extraCommentView=[[UIView alloc]initWithFrame:CGRectMake(0,y,280,198)];
_extraCommentView.tag=commentCount;
UITextView *commentTextView=[[UITextView alloc]initWithFrame:CGRectMake(0, 29, 280, 134)];
commentTextView.backgroundColor=[UIColor colorWithRed:(195.0/255.0) green:(195.0/255.0) blue:(195.0/255.0) alpha:1];
[_extraCommentView addSubview:commentTextView];
UIView *previous=(UIView *)[_extraCommentView viewWithTag:1];
UIView *next=(UIView *)[_extraCommentView viewWithTag:2];
NSLog(@"prev ->%@",previous);
NSLog(@"nxt ->%@",next);
commentCount++;
y+=200
}
我尝试使用标签“1”和“2”访问UIViews。例如,当我点击UIButton 4 次时,这个块执行了 4 次,我得到了这个日志:
2014-04-03 16:24:15.106 SmartWatch[2465:70b] pre1-><UIView: 0x8c69440; frame = (0 -396; 280 198); tag = 1; layer = <CALayer: 0x8c6b5e0>>
2014-04-03 16:24:15.107 SmartWatch[2465:70b] nxt1->(null)
2014-04-03 16:24:16.450 SmartWatch[2465:70b] pre1->(null)
2014-04-03 16:24:16.450 SmartWatch[2465:70b] nxt1-><UIView: 0x8c4f2f0; frame = (0 -198; 280 198); tag = 2; layer = <CALayer: 0x8c55ca0>>
2014-04-03 16:24:16.642 SmartWatch[2465:70b] pre1->(null)
2014-04-03 16:24:16.642 SmartWatch[2465:70b] nxt1->(null)
2014-04-03 16:24:16.945 SmartWatch[2465:70b] pre1->(null)
2014-04-03 16:24:16.946 SmartWatch[2465:70b] nxt1->(null)
为什么带有标签“1”和“2”的UIViews 在第 3 次和第 4 次点击时返回 NULL?
【问题讨论】:
标签: ios iphone objective-c uiview tags