xcode Swift - 检索子视图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26086175/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Swift - Retrieving subviews
提问by user2961953
In my application I am adding labels to the view and then I am attempting to clear particular labels from the view when a button is clicked and am running into an error when trying to retrieve the subviews:
在我的应用程序中,我正在向视图添加标签,然后我试图在单击按钮时从视图中清除特定标签,并在尝试检索子视图时遇到错误:
class FirstViewController: UIViewController {
@IBAction func btnAddTask_Click(sender: UIButton){
var subViews = self.subviews.copy()
}
}
I get the error:
我收到错误:
'FirstViewController' does not have a member named 'subviews'
“FirstViewController”没有名为“subviews”的成员
How can I get the subviews of the current view?
如何获取当前视图的子视图?
回答by Rob Napier
UIViewController
doesn't have a subviews
property. It has a view
property, which has a subviews
property:
UIViewController
没有subviews
财产。它有一个view
属性,它有一个subviews
属性:
for subview in self.view.subviews {
// Manipulate the view
}
But typically this is not a good idea. You should instead put the labels you want into an IBOutletCollection
and iterate over that. Otherwise you've very tied to the exact set of subviews (which may change).
但通常这不是一个好主意。相反,您应该将所需的标签放入 anIBOutletCollection
并对其进行迭代。否则,您与确切的子视图集(可能会更改)非常相关。
To create the IBOutletCollection
, select all the labels you want in IB, and control-drag them to the source code. It should ask if you want to make a collection array. (Note that there is no promise on the order of this array.)
要创建IBOutletCollection
,请在 IB 中选择您想要的所有标签,然后按住 Control 键将它们拖到源代码中。它应该询问您是否要创建一个集合数组。(请注意,此数组的顺序没有承诺。)
回答by Ivan Trubnikov
For find all subviews from your view you can use this code:
要从您的视图中查找所有子视图,您可以使用以下代码:
for subview in self.view.subviews {
// Use your subview as you want
}
But for use it, you must identify view what you need. You can mark any element, what you create, with special Identifier like this:
但是要使用它,您必须确定您需要的视图。您可以使用特殊标识符标记您创建的任何元素,如下所示:
myButton.restorationIdentifier = "mySpecialButton";
And after that you can find your element use this structure:
之后你可以找到你的元素使用这个结构:
for view in view.subviews {
if (view.restorationIdentifier == "mySpecialButton") {
print("I FIND IT");
view.removeFromSuperview();
}
}
:)
:)
回答by Saam Barrager
Hacky elegance:
哈克优雅:
- Get the position of the click.
- Retrieve the view with hitTest.
- Differentiate the view with accessibilityIdentifier.
- 获取点击位置。
- 使用 hitTest 检索视图。
- 用accessibilityIdentifier 区分视图。
accessibilityIdentifier is developer facing and was intended for UI Automation.
可访问性标识符面向开发人员,旨在用于 UI 自动化。
Swift 4
斯威夫特 4
@objc func handleTap(_ gestureRecognizer: UITapGestureRecognizer) {
let positionInView = gestureRecognizer.location(in: view)
let hitTestView = view?.hitTest(positionInView, with: nil)
print("hitTestView: \(hitTestView?.accessibilityIdentifier)")
}