【问题标题】:Already created UIViews with tags becomes NULL已经创建的带有标签的 UIViews 变为 NULL
【发布时间】: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


    【解决方案1】:

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

    _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
    

    所以回答你的问题:

    为什么带有标签 '1' 和 '2' 的 UIView 在第 3 和第 4 时返回 NULL 点击?

    在第 3 和第 4 按钮中单击 commentCount 等于 3 和 for 但 NSLogs 日志视图仅针对标记 1 和 2。

    【讨论】:

      【解决方案2】:

      @Greg 的回答是对的,如果你想要一个视图列表尝试添加到一个数组中

       NSMutableArray *viewArray=[[NSMutableArray alloc]init];
      - (IBAction) OnClickAddAnother:(id)sender 
      {
      
           UIView *_extraCommentView=[[[UIView alloc]init ]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];
      
          [viewArray addObject:_extraCommentView];
          for(UIView *view in viewArray )
          {
              NSLog(@"View  ->%@",view);
          }
      
          commentCount++;
          y+=200;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-06-22
        • 1970-01-01
        • 2010-10-30
        • 2011-12-25
        相关资源
        最近更新 更多