ios - How can I call a function from an existing View Controller class? -


i'm still stage swift developer. i'm trying create feature click button on screen, prompted iphone allow location, , when click allow, automatically navigated screen.

so far i've done following things:

  • created seperate class ("user") location functions handled
  • setup button on first view controller , appropiate ibaction calls location prompt function "user"
  • added storyboard ids first , second view controller
  • created function ("changescreen") in first view controller class performs navigation view
  • setup listener in "user" class when user has clicked allow location, calls "changescreen" function

i think there's way (call sort of completion handler) , toyed around 1-2 hours , nothing worked. far solution has worked following error:

warning: attempt present ____ on _____ view not in window hierarchy!

here's code in "user" class

class user: nsobject, cllocationmanagerdelegate {  func getlocation(){      // use in foreground     locationmanager.requestwheninuseauthorization()      locationmanager.delegate = self     locationmanager.desiredaccuracy = kcllocationaccuracybest     locationmanager.requestwheninuseauthorization()     //locationmanager.startupdatinglocation()      println("hello location")   }      func locationmanager(manager: cllocationmanager, didchangeauthorizationstatus status:clauthorizationstatus){          if status == .authorizedwheninuse {              locationmanager.startupdatinglocation()              firstview!.showsecondscreen() // call other      class's function          }     }   } 

here's code in first view controller class

var firstview: firstview? = nil  class firstview: uiviewcontroller {  var newuser = user()  override func viewdidappear(animated: bool) {      firstview = self.storyboard!.instantiateviewcontrollerwithidentifier("firstview") as? firstview  // here take first view controller class , store public variable  }  @ibaction func allowlocationbtn(sender: anyobject) {      newuser.getlocation()  // first button clicked  }  func showsecondscreen(){      let secondscreen = self.storyboard!.instantiateviewcontrollerwithidentifier("secondscreen") as! secondscreen      self.presentviewcontroller(secondscreen, animated: true, completion:   nil  )  } 

ps. know can combine code 1 class, , work that. want "proper" way, have different functionalities in different classes.

  • try set first = self , dont instantiate new view-controller
  • if using storyboard, better use segues display screen

though correct thing here have userdelegate protocol

protocol userdelegate { func displayotherscreen( ) }

then in user add var delegate: userdelegate? , in locationmanager function instead of calling firstview!.showsecondscreen call delegate?.displayotherscreen( )

then make firstview adopt userdelegate protocol

class firstview: uiviewcontroller, userdelegate { var newuser = user()  override func viewdidappear(animated: bool) {     newuser.delegate = self     ...     }     ...     ... func displayotherscreen( ) {     showsecondsceen( )      } 

now there no need first variable..


Comments

Popular posts from this blog

python - Healpy: From Data to Healpix map -

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -