0

As I read in another question, a rule of thumb was:

When a parent has a reference to a child object, you should use a strong reference. When a child has a reference to its parent object, you should use a weak reference or a unsafe_unretained one (if the former is not available). A typical scenario is when you deal with delegates. For example, a UITableViewDelegate doesn't retain a controller class that contains a table view.

Well, in this case I have a view controller with a table view IBOutlet. This table view also uses the view controller as a delegate and data source. By the rule above, the view controller acts as the parent and so I should have a strong reference to the table view - the table view should have a weak reference to the view controller.

I'm not sure how to define that latter weak reference - I'm setting

tableView.delegate = self;
tableView.dataSource = self;

How am I supposed to make myself weak? Or is this not the right way to do it?

Eric Gao
  • 3,502
  • 2
  • 19
  • 29
  • 1
    Just drag the outlet from your TableView in storyboard to your .h file. Xcode will take care of making it a weak IBOutlet – NSNoob Feb 03 '16 at 12:06

4 Answers4

2

You can just drag the outlet from your UITableView to your .h file. Xcode will create appropriate property for you. You can choose weak/strong type from there as well while naming the Outlet.

If you would however like to create it manually, you can add this line to your .h file:

@property (weak, nonatomic) IBOutlet UITableView *tblView;

But do keep in mind that even after adding this line to your .h. file, you will still have to connect it to your UITableView.

Also don't forget to make your ViewController conform to UITableViewDelegate like below:

@interface MyTableViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>

In your title however, you seem to be confused about the difference between Weak and Strong properties. Ole Begemann has answered about the difference splendidly on This question.

Community
  • 1
  • 1
NSNoob
  • 5,548
  • 6
  • 41
  • 54
1

The code you wrote is fine. You don't have to make yourself weak. The delegate and datasource properties are declare as weak in the UITableView definition.

marosoaie
  • 2,352
  • 23
  • 32
0

delegate and datasource are already weak as per UITableView.h.

@property (nonatomic, weak, nullable) id <UITableViewDataSource> dataSource;
@property (nonatomic, weak, nullable) id <UITableViewDelegate> delegate;

If any instance is set as delegate and datasource won't get retained by tableview instance

And when you make a IBOutlet make it like this. Choose weak if its strong by default its weak as shown in image

@property (weak, nonatomic) IBOutlet UITableView *tableview;

enter image description here

As the view is already retained by the view it's added (i.e self.view)

Rajesh
  • 10,318
  • 16
  • 44
  • 64
0

It is just like a view in UIKit Control to display data in list form. It is not a child controller. All UIKit items are referenced as weak.

Shehzad Ali
  • 1,846
  • 10
  • 16