title: objective-c protocol as variable (instance)
body: <p>I'm new to objective-c and it is still unclear to me of the following are possible</p>
  
  <ol>
  <li>Is it possible to instantiate a protocol?</li>
  <li>If it is possible to instantiate a protocol, how do I implement the required methods?</li>
  </ol>
  
  <p><strong>EDITS:</strong></p>
  
  <p>This is how I used and implemented my protocol:</p>
  
  <pre><code>@protocol myProtocol &lt;NSObject&gt;
  
  @required
      - (void)doneAction:(NSDate *)dateSelected;
  @end
  
  @interface datePickerView : UIView
  
  @property (strong, nonatomic) UIDatePicker *datePicker;
  @property (strong, nonatomic) UIButton *doneButton;
  @property (strong, nonatomic) NSObject&lt;myProtocol&gt; *datePickerProtocol;
  
  - (id)initWithDetails:(UIDatePicker *)datePick doneBtn:(UIButton *)doneBtn;
  - (void)show;
  @end
  </code></pre>
  
  <p>Here is the implementation of "datePickerView"</p>
  
  <pre><code>@synthesize datePicker = _datePicker;
  @synthesize doneButton = _doneButton;
  @synthesize datePickerProtocol = _datePickerProtocol;
  
  - (id)initWithDetails:(UIDatePicker *)datePick doneBtn:(UIButton *)doneBtn
  {
      _datePicker = datePick;
      _doneButton = doneBtn;
      [_doneButton addTarget:self action:@selector(doneButtonClicked) forControlEvents:UIControlEventTouchUpInside];
  
      [self addSubview:_datePicker];
      [self addSubview:_doneButton];
  }
  
  - (void)doneButtonClicked
  {
      /** Code to hide the view goes here **/
      if (_datePickerProtocol != nil)
          [_datePickerProtocol doneAction:_datePicker.date];
  }
  </code></pre>
  
  <p>Here is the class that is using the "datePickerView"</p>
  
  <pre><code>/** .h file **/
  @interface myController : UITableViewController &lt;myProtocol&gt;
  {
  }
  
  @property (strong, nonatomic) IBOutlet UIDatePicker *datePicker;
  @property (strong, nonatomic) IBOutlet UIButton *datePickDone;
  /** More definitions goes here **/
  
  /** .m file **/
  @synthesize datePicker = _datePicker;
  @synthesize datePickDone = _datePickDone;
  /** More code goes here **/
  
  - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  {
      UITableViewCell *selectedCell = [self.tableView cellForRowAtIndexPath:indexPath];
      if ((![selectedCell.reuseIdentifier isEqualToString:@"dateAppliedCellIdentifier"]) &amp;&amp;
          (![selectedCell.reuseIdentifier isEqualToString:@"closingDateCellIdentifier"]))
          return;
  
      if (_datePicker.superview == nil)
      {
          datePickerView = [[datePickerView alloc] initWithDetails:_datePicker     doneBtn:_datePickDone];
  
          [self.view.window addSubview: datePickerView];
      }
      /**
          THIS IS WHERE I WANT TO PASS AN INSTANCE OF THE PROTOCOL (BUT AS ADVISED,
          IT IS NOT POSSIBLE) DEPENDING ON WHICH CELL WAS TAPPED. THE REASON I WANT
          PASS AN INSTANCE OF THE PROTOCOL INSTEAD OF "SELF" IS BECAUSE I WANT
          TO AVOID DOING MULTIPLE "IF" STATEMENTS INSIDE
          "(void)doneAction:(NSDate *)dateSelected" METHOD. I WOULD LIKE TO DO THE IFs
          HERE AND PASS THE CORRECT INSTANCE OF THE PROTOCOL DEPENDING ON WHICH CELL
          WAS TAPPED. EACH INSTANCE OF THE PROTOCOLS HAVE DIFFERENT IMPLEMENTATIONS OF
          "doneAction".
      **/
      [datePickerView setDatePickerProtocol:self];
      [datePickerView show];
  }
  
  //implementation of the protocol method
  - (void)doneAction:(NSDate *)dateSelected
  {
      //IF STATEMENTS GOES HERE DEPENDING ON WHICH CELL WAS TAPPED
      NSLog(@"Done action is called!");
  }
  </code></pre>
  
  <p>Please see my comments on the code, above this line [datePickerView setDatePickerProtocol:self]. Since I cannot instantiate protocols and assign to variables, is there an alternative or a better way of achieving my goal aside from implementing "myProtocol" from the controller and doing multiple IF statements inside "doneAction" method?</p>
  
  <p>Based from the answers, it seems like I need to define classes that implements "myProtocol" and create instance of those classes and pass to "datePickerView setDatePickerProtocol". Is my understanding correct or is this reasonable to do?</p>