2012-12-13 40 views
1

可能重複:
Difference beetween _ and self. in Objective-C我應該使用self.tableView還是僅使用tableView?

我應該使用self.tableView或只是通過的tableView本身。

在我的代碼我注意到,這兩個

[self.view addSubview:self.tableView]; 

[self.view addSubview:tableView]; 

都工作。我的tableView添加了IB,並設置了委託和數據源和出口。

感謝

+0

thai bhai thai avu mare pn 1 var thayu hatu :( – 2012-12-13 12:09:42

+1

當你做self.tableView時,幕後發生的事情是[self getTableView]被調用,所以儘管99%的時間,你會得到你想要什麼,有些情況下,getProperty implmentation也可以做一些額外的事情,它基本上是訪問一個屬性和一個變量的區別。 – Srikanth

回答

1

如果你聲明你的表,而不是讓喜歡

IBOutlet UITableView *table; 

那麼它的屬性,你可以訪問你的tableView

[self.view addSubView:tableView]; 

,如果你設置它的getter和setter物業然後你可以打電話使用self.table

@property(nonatomic,retain)IBOutlet UITableView *table; 

然後你就可以通過

[self.view addSubView:self.table]; 

訪問欲瞭解更多信息,你可以閱讀UITableViewController Class Reference

0

只是增加了上面的回答

如果你的表是由一個屬性,則最好是總是使用

[self.view addSubview:self.tableView]; 

這樣做的好處之一是當合成大小作爲訪問器爲其生成的屬性,而在另一種方法中,這不會發生。如果財產被保留,保留和釋放將由訪問者完成。

從個人經驗,我建議你至少可以避免內存訪問違規。

相關問題