Is it possible to associate a delegate and dataSource CustomViews in an interface builder?

In the interface builder. If I right-click on the tableView, I get the option of delegation points and dataSource, which from time to time we connect to the owner of the file, which in most cases is the View Controller that implements this protocol.

How can I get a similar option for my custom view that has a delegate and datasource property?

+6
source share
2 answers

You must fulfill the following conditions:

  • In the Custom Class view, your custom view class name must be set in Interface Builder (via Identity Inspector). If your delegate or dataSource object is also a custom view, also make sure this kind of custom class is set
  • @interface for your custom class should beautify its delegate and dataSource IBOutlet properties. For example, @property (nonatomic, weak) IBOutlet id <SomeProtocol> delegate;
  • If you have declared a protocol for your delegate or data source, the target you want to use as a delegate or data source must be declared as implementing this protocol
+16
source

You can achieve this by following these steps.

  • create a new subclass of UIview
  • in the .h file, the delegate property and the datasource property are set, for example

    UIViewController

  • then we implement the protocols of delegates and data sources in your .m file. as usual.

  • OK Now in your view the controller drags the view into the interface constructor or creates an instance programmatically.

  • set the class of your view to the subclass that you created. (on the right side of the interface);

  • then put the UITableView inside this view. and drag the connections to the parent UIview and select the data source and delegates.

  • All this is done now, your subclass of UIview will act as a data source and a delegate to represent the table.
-1
source

Source: https://habr.com/ru/post/950963/


All Articles