简体   繁体   中英

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 .

The problem: I can't make the buttons on the xib interact with other subviews (eg to change the text of a 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; 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM