ios - Reuse same view in list and detail ViewController -
i have common list-detail app. tableview displaying index of items , detail viewcontroller showing single item more details.
list: +-----------------+ | | | image 1 | | | +-----------------+ |button1 button2 | |=================| | | | image 2 | | | +-----------------+ |button1 button2 | |=================| | . | | . | | . | detail: +-----------------+ | | | image 1 | | | +-----------------+ | text | +-----------------+ |button1 button2 | <= same actions/handlers in list |=================|
both have button bar multiple buttons (like, save, etc). how reuse logic of button bar?
you want add functions (save, etc) class contains data (image, text etc). can call function anywhere want , same. example:
class post { var image: uiimage? var text: string? func save() { // put code here save 'post' } func like() { // put code here 'post' } }
for custom uiview
, uitableviewcell
can similar thing using protocol
, extension
. example:
protocol updatestate { var myimageview: uiimageview? { } var mytextlabel: uilabel? { } func save() func like() } extension updatestate { func save() { // put code here save 'post' mytextlabel?.text = "new text after save" myimageview?.image = uiimage(named: "savedimage") } func like() { // put code here 'post' mytextlabel?.text = "new text after like" myimageview?.image = uiimage(named: "newimage") } } class cell: uitableviewcell, updatestate { @iboutlet weak var myimageview: uiimageview? // here conform updatestate protocol var mytextlabel: uilabel? = nil } class view: uiview, updatestate { @iboutlet weak var myimageview: uiimageview? @iboutlet weak var mytextlabel: uilabel? }
Comments
Post a Comment