使用标记隐藏子视图

时间:2014-07-11 19:47:04

标签: ios swift

在swift中,我可以为视图提供标签

  let MY_CUSTOM_TAG = 123
  tableView.tag = MY_CUSTOM_TAG

我的问题是,如何使用swift从标签中删除超级视图中的视图?

目标C示例:

#define MY_CUSTOM_TAG 1234
mySubview.tag = MY_CUSTOM_TAG;
[self.tableView addSubview:mySubview] ;

//remove view with tag

[[self.tableView viewWithTag:MY_CUSTOM_TAG]removeFromSuperview] ;

2 个答案:

答案 0 :(得分:11)

与Objective-C的方式相同,只是有不同的语法;

view.viewWithTag(tag).removeFromSuperview()

答案 1 :(得分:3)

对于那些来这里寻找隐藏子视图的解决方案的人。

<强>目标-C:

[[self.view viewWithTag:MY_CUSTOM_TAG] setHidden: YES];

<强>夫特:

self.view.viewWithTag(viewWithTag:MY_CUSTOM_TAG)?.hidden = true

P.D:因为标题询问如何隐藏子视图而不是如何删除它。