简体   繁体   中英

Push another Viewcontroller from action button in xib file

I have a viewcontroller that presents from a .xib . Inside the .xib there is a button; I want the button to pushviewcontroller to an other viewcontroller ( ViewControllerPeopleNew ).

I use an action button:

@IBAction func _hitPeople(sender: AnyObject) {
    let mapViewControllerObejct = self.storyboard?.instantiateViewControllerWithIdentifier("peoplenew") as? ViewControllerPeopleNew
    self.navigationController?.pushViewController(mapViewControllerObejct!, animated: false)
}

But got an error:

Signal SIGABRT
unrecognized selector sent to instance

I already checked all about name identifier viewcontroller and all is right.

From view controller present with XIB you can not get storyboad use below code

@IBAction func _hitPeople(sender: AnyObject) {

let storyboard = UIStoryboard(name: "storyboadname", bundle: nil)
let mapViewControllerObejct = storyboard.instantiateViewControllerWithIdentifier("peoplenew") as? ViewControllerPeopleNew
self.navigationController?.pushViewController(mapViewControllerObejct!, animated: false)
}

Always remember that you can only use self.storyBoard to instantiate a view controller when both the view controllers ( To and From view controllers ) belong to the same storyboard . If not then you need to first get a reference to the storyboard your To view controller belongs to. So you need to do this:

@IBAction func _hitPeople(sender: AnyObject) {
    let stotyboard = UIStoryboard(name: "YourStoryboard", bundle: nil)
    let mapViewControllerObejct = storyboard?.instantiateViewControllerWithIdentifier("peoplenew") as? ViewControllerPeopleNew
    self.navigationController?.pushViewController(mapViewControllerObejct!, animated: false)
}

PS: Just be sure that your From view controller is in the navigation stack ie self.navigationController is not nil .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM