简体   繁体   English

如何在Swift中使Xib子视图与超级视图交互?

[英]How to make a xib subview interact with superview in Swift?

I've been trying this for days... 我已经尝试了好几天了...

I want to have a nib subview with some buttons and other stuff. 我想要一个带有一些按钮和其他内容的笔尖子视图。 The main view would have other subviews that change in response to pressing the buttons of the nib view. 主视图将具有其他子视图,这些子视图响应于按下笔尖视图的按钮而改变。

What I can do: I already loaded the xib subview into the superview using this approach . 我可以做什么:我已经使用这种方法将xib子视图加载到了超级视图中。

The problem: I can't make the buttons on the xib interact with other subviews (eg to change the text of a Label) 问题:我无法使xib上的按钮与其他子视图交互(例如,更改Label的文本)

What I haven't nailed yet is how to structure the files and their relations. 我还没有提到的是如何构造文件及其关系。

You could use addTarget:action:forControlEvents: after you load the nib to add the action methods for your buttons; 您可以使用addTarget:action:forControlEvents:加载笔尖后,为按钮添加操作方法。 the target should be self (the controller where you've added this view). 目标应该是self(添加此视图的控制器)。 The view should have IBOutlets for any buttons or other controls you need to interact with. 该视图应具有IBOutlets,用于您需要与之交互的任何按钮或其他控件。 You shouldn't have any actions for the buttons in the subview class if you use this approach. 如果使用此方法,则子视图类中的按钮不应有任何操作。

Another approach, would be to have the action methods for the buttons in the subview class, and have those methods call methods of a delegate protocol that you create in the subview class. 另一种方法是在子视图类中具有按钮的操作方法,并让这些方法调用您在子视图类中创建的委托协议的方法。 When you add the subview to your view, you would set the controller as the delegate of the subview, and implement any protocol methods that you defined. 将子视图添加到视图时,可以将控制器设置为子视图的委托,并实现您定义的任何协议方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何添加具有约束的子视图以使子视图具有与其父视图相同的框架? - How to add a subview with constraints to make the subview with the same frame as its superview? 如何快速将其子视图塑造为子视图? - How can I shape a subview to it's superview in swift? 如何在子视图块中使UIGestureRecognizer在容器Superview中的UIGestureRecognizer - How to make UIGestureRecognizer in subview block UIGestureRecognizer in container superview 如何使超级视图自动调整大小并与其子视图保持一致? - How to make a superview resize automatically same size and coordinate with its subview? 如何使以编程方式生成的子视图符合超级视图(iOS)的范围? - how to make a programmatically generated subview conform to the bounds of the superview (iOS)? Swift:我们如何从 Xcode 中的 xib 中提取子视图? - Swift: How can we extract a subview from xib in Xcode? 如何通知superview删除子视图 - How to notify superview of subview removal ios / swift-在SuperView后面放置一个子视图? - ios/swift - Putting a subview behind the superView? 如何从超级视图的超级视图中将subView置于顶部 - How to bring subView to top from superview of its superview 添加Xib TableView单元的子视图(Swift) - Add subview of xib tableview cell (swift)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM