UITableView to programmatically create a delegate object?

I have a question regarding setting up a custom delegate class for use with a UITableView. I did the following:

  • Install a new class (in separate * .h and * .m files for the class)
  • Conforms to this new class of protocols <UITableViewDelegate, UITableViewDataSource>

  • Added required methods.
  • Created a pointer to a new object using @property and IBOutlet.
  • In InterfaceBuilder created and assigned an object template for my new class
  • Data source assigned and connections delegated.

This all works great. My question is, if I don't want to use an interfaceBuilder to set and instantiate my new delegate class, how should I do it in Xcode instead? More specifically, how would I:

  • Instantiating the delegate class, will it be instantiated / owned by the controller?
  • Set up datasource and delegate connections?
  • What's the best way to do this?
+2


a source to share


1 answer


In your view controller create a new instance of the delegate class and assign it to your property, make sure you remove IBOutlet

MyDelegateClass * delegateClass = [[MyDelegateClass alloc] init];
[self setMyProperty:delegateClass];
[delegateClass release];

      

You can set a set of tableview delegate and datasource by doing the following:



[myTableView setDelegate:self.myProperty];
[myTableView setDatasource:self.myProperty];

      

You should probably put it all in viewDidLoad

+3


a source







All Articles