gpt4 book ai didi

ios - 已经创建的带有标签的 UIViews 变为 NULL

转载 作者:行者123 更新时间:2023-11-28 18:33:57 28 4
gpt4 key购买 nike

我有一个 UIButton,单击它时,将执行以下 block 。此 block 创建一个 UITextView 并将其作为 subview 添加到带有 tag 的新 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 次时,此 block 执行 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?

最佳答案

您的代码在您创建的第一次迭代中运行良好:

_extraCommentView with tag = 1 
commentTextView no tag

结果是

tag 1 - _extraCommentView which is right
tag 2 is NULL which is also right.

在您创建的第二次迭代中

_extraCommentView with tag = 2
commentTextView no tag

结果是:

tag 1 null 
tag 2 _extraCommentView

这也是对的。

在您创建的第 3 次迭代中

  _extraCommentView with tag = 3
commentTextView` no tag

结果是:

tag 1 null 
tag 2 null

所以回答你的问题:

why does UIViews with tag '1' and '2' return NULL for 3rd and 4th clicks?

在第 3 个和第 4 个按钮中,单击 commentCount 等于 3,但是 NSLogs 日志 View 仅针对标记 1 和 2。

关于ios - 已经创建的带有标签的 UIViews 变为 NULL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22836086/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com