ios - Present Popover View Controller Swift -


when try display popover view controller programmatically won't work , don't know why. i've copied multiple sources on web , nothing seems work, same error in console every time showing warning: attempt present <addfriendspopoverviewcontroller> on <mainpageviewcontroller> view not in window hierarchy! lost , can't seem figure out problem is, in advance!

here swift code in viewdidload() function:

let addfriendspopoverviewcontroller = addfriendspopoverviewcontroller()  override func viewdidload() {     super.viewdidload()      if (pfuser.currentuser()?["numberoffriends"])! as! nsobject == 0 {         print(pfuser.currentuser()?["numberoffriends"])          let storyboard = uistoryboard(name: "main", bundle: nil)         let vc = storyboard.instantiateviewcontrollerwithidentifier("addfriendspopoverviewcontroller") as! uiviewcontroller          vc.modalpresentationstyle = uimodalpresentationstyle.popover         vc.preferredcontentsize = cgsizemake(50, 50)         let popovermenuviewcontroller = vc.popoverpresentationcontroller         popovermenuviewcontroller!.permittedarrowdirections = .any         popovermenuviewcontroller!.delegate = self         popovermenuviewcontroller!.sourceview = self.view         popovermenuviewcontroller!.sourcerect = cgrectmake(             100,             100,             0,             0)      self.presentviewcontroller(vc, animated: true, completion: nil)      }  } 

edit figured out popover work iphone following code required.

func adaptivepresentationstyleforpresentationcontroller(controller: uipresentationcontroller!) -> uimodalpresentationstyle {     // return no adaptive presentation style, use default presentation behaviour     return .none } 

your view not in view hierarchy until has been presented , not during viewdidload:

move code viewdidappear:

 if (pfuser.currentuser()?["numberoffriends"])! as! nsobject == 0 {      addfriendspopoverviewcontroller.modalpresentationstyle =   uimodalpresentationstyle.popover     addfriendspopoverviewcontroller.preferredcontentsize = cgsizemake(200, 200)     let popovermenuviewcontroller = addfriendspopoverviewcontroller.popoverpresentationcontroller     popovermenuviewcontroller!.permittedarrowdirections = .any     popovermenuviewcontroller!.delegate = self     popovermenuviewcontroller!.sourceview = self.view     popovermenuviewcontroller!.sourcerect = cgrect(         x: 50,         y: 50,         width: 1,         height: 1)     presentviewcontroller(         addfriendspopoverviewcontroller,         animated: true,         completion: nil)  } 

Comments

Popular posts from this blog

java - UnknownEntityTypeException: Unable to locate persister (Hibernate 5.0) -

python - ValueError: empty vocabulary; perhaps the documents only contain stop words -

ubuntu - collect2: fatal error: ld terminated with signal 9 [Killed] -