ios - Call a function in another class that read a dynamic variable via IBaction -
i'm trying call function stored on class:
viewcontroller
@ibaction func refreshcomments() { commentstableviewcontroller().refresh() }
commentstableviewcontroller
var variable2 = "0" func refresh(){ print(variable2) }
this works great, in case value of variable not static value, i've receive dynamically value, , know problem in line :
//commentstableviewcontroller().refresh()
because commentstableviewcontroller()
- class constructor , each time call commentstableviewcontroller()
return new instance/object of commentstableviewcontroller
class, default values of course.
so i'm stuck @ point , don't know how solve this. i've tried transform func refresh()
class funcrefresh()
, call way :
@ibaction func refreshcomments() { commentstableviewcontroller.refresh() }
but in way got error i'm not able read variable2
print(variable2) - instance member 'variable2'cannot used on type 'commentstableviewcontroller'
any ideas?
handling controller controller in container bit difficult. try fill container programaticly. otherwise not have controll on controller.
you work notifications, notifications shouldnt used 1 one communication.
create programaticly
- you have container , link iboutlet maincontroller.
- you create property commentsviewcontroller
- in storyboard give controller storyboardidentifier
- in viewdidload of main controller instanciate commentscontroller property controller storyboard.
- add controller childviewcontroller maincontroller , add view commentsviewcontroller containerview
- could need set view.frame commentsviewcontroller same containerview
now have controller property , can run public function.
sorry no code example. got not time :)
notifcations
your main view controller send notification specific name , commentsviewcontroller observer notification , make action when receiving it.
nsnotificationcenter.defaultcenter().postnotificationname(name:"yournameyoulike")
in commentsviewcontroller observe notification.
nsnotificationcenter.defaultcenter().addobserverforname(name:"thesamenamethatyourpostnotificationdiduse", object: nil, queue: mainqueue) { [weak self] in guard s = self else { return } s.reload() }
Comments
Post a Comment