iphone - why and where to use a delegate in objective c -
i’m new iphone programmer
i have created small views without delegates.... if program can run without delegate, why use them. need them, have seen programs on net having delegates.
i’m confused, please answer.
delegates pattern allows object notify object when stuff happens. lots of delegate methods start "will" or "did" called before , after event occurs, respectively.
let's have uitableview
, , want know when user taps cell can process choice. assign delegate table view implements delegate method tableview:didselectrowatindexpath:
.
the nice things approach table view doesn't have know or care other delegate object or how works. allows object generating events stay focused , clean, dealing it's own state. when noteworthy, send message it's delegate. , delegate object doesn't need know internal state of other object @ all. needs know did important.
and fits model/view/controller (mvc) approach of iphone sdk well. view should concerned how displays itself, , controller delegate of view when things change.
programming delegates not required language feature objective-c. it's merely design pattern sdk uses quite bit. it's pattern can keep code cleaner.
Comments
Post a Comment