Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to connect delegate and dataSource of CustomViews in interface builder?

In Interface builder, If I right click on a tableView, I get option of delegate and dataSource outlets which at times we connect to the file's owner which is in most cases the View Controller which implements these protocol,

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

like image 586
Amogh Talpallikar Avatar asked Aug 03 '13 13:08

Amogh Talpallikar


2 Answers

You'll need to meet these conditions:

  1. The view's Custom Class should be set to your custom view's class name in Interface Builder (via the Identity Inspector). If your delegate or dataSource object is also a custom view, also make sure that that view's Custom Class is set
  2. The @interface for your custom class should decorate its delegate and dataSource properties with IBOutlet. For example, @property (nonatomic, weak) IBOutlet id <SomeProtocol> delegate;
  3. If you declared protocol(s) for your delegate or dataSource, the target object that you want to use as the delegate or dataSource should be declared as implementing that protocol
like image 114
Mike Mertsock Avatar answered Oct 17 '22 01:10

Mike Mertsock


Create your custom delegate,

@objc protocol CustomDelegate: class {
func itemSelected(_ success: Bool)
}

Create your custom class and property of your custom delegate (Make sure you add @IBOutlet during the property declaration),

class CustomView: UIView {
@IBOutlet weak var cDelegate: CustomDelegate!
//...
//...
//...
}

Now, Go to the Storyboard where you want to add this custom view, Take a UIView and change the class then right click on it, you will able to see the property ("cDelegate") you declared. See the picture for better understanding,

enter image description here

like image 2
Soumen Avatar answered Oct 17 '22 03:10

Soumen