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

568 views Asked by At

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.

The problem: I can't make the buttons on the xib interact with other subviews (e.g. to change the text of a Label)

What I haven't nailed yet is how to structure the files and their relations.

1

There are 1 answers

1
rdelmar On

You could use addTarget:action:forControlEvents: after you load the nib to add the action methods for your buttons; the target should be self (the controller where you've added this view). The view should have IBOutlets for any buttons or other controls you need to interact with. 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.