ios - How to call method of my ViewController from separate non UIViewController Class -
what im trying networkcall(asyncronous call) in seprate class file. when network call completed should throw function callback in actuall viewcontroller , update ui-views
i have facilitiesnewvc.swift class this
class facilitiesnewvc: uiviewcontroller, roomsdataprotocol { var data = nsmutabledata() var roomsdata = roomsdata() override func viewdidload() { super.viewdidload() // additional setup after loading view. //image background roomsdata.startconnection(); } // overriden method (not called) func didfinishloadingurl(jsonresult:nsdictionary) -> void{ //this method not getting called println("in view controller") } }
note: roomsdata.startconnection();
here roomsdata.swift
class roomsdata: nsobject, nsurlconnectiondelegate { var data = nsmutabledata() var callback:roomsdataprotocol? func startconnection(){ let urlpath: string = "http://blue.genetechz.com/qadir/bed_room.php" var url: nsurl = nsurl(string: urlpath)! var request: nsurlrequest = nsurlrequest(url: url) var connection: nsurlconnection = nsurlconnection(request: request, delegate: self, startimmediately: false)! println("startconnection"); connection.start() } func connection(connection: nsurlconnection!, didreceivedata data: nsdata!){ self.data.appenddata(data) println("connection"); } func connectiondidfinishloading(connection: nsurlconnection!) { println("connectiondidfinishloading"); var err: nserror var jsonresult: nsdictionary = nsjsonserialization.jsonobjectwithdata(data, options: nsjsonreadingoptions.mutablecontainers, error: nil) nsdictionary callback?.didfinishloadingurl(jsonresult); // call overriden method in facilitiesnewvc println("roomsdata") } }
and lastly here protocol i.e roomsdataprotocol
protocol roomsdataprotocol { func didfinishloadingurl(jsondata:nsdictionary) }
why overriden method in facilitiesnewvc.swift class (i.e didfinishloadingurl() ) not getting called?
in code posted, there no line sets callback
variable in roomsdata
class. ?.
operator not call method in line
callback?.didfinishloadingurl(jsonresult);
i suggest add
roomsdata.callback = self
somewhere before calling
roomsdata.startconnection();
in facilitiesnewvc
class.
Comments
Post a Comment