[英]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.