ios - scrollView setContentOffset crashes when accessing method from another file -
i have homeview class scrollview iboutlet , function changes offset of scrollview:
scrollview.setcontentoffset(cgpoint(x: self.view.frame.width * 2, y: 0), animated: true) from feedview class attempt:
let home = homeview() home.scrollright() but error:
fatal error: unexpectedly found nil while unwrapping optional value
because homeview created either xib or story board. thats why below line return nil object.
let home = homeview() if want call scrollright() method of homeview class declare global variable of homeview
var homevc = homeview() and in viewdidmethod of homeview
homevc = self homeview not can access homevc anywhere
and call method
homevc.scrollright() from viewcontroller.
=======================================
ex
import uikit var homevc = homevc() class homevc: uiviewcontroller { override func viewdidload() { super.viewdidload() homevc = self homevc } override func didreceivememorywarning() { super.didreceivememorywarning() } } now can use homevc object , access property of homevc anywhere.
note: if calling function same viewcontroller (homeview) don't need create object of viewcontroller. can call function below directly.
scrollright() or self.scrollright()
Comments
Post a Comment