0

I have a parentview which has 2 child views, I am trying to show a uiviewcontroller as "Present modally" I use the following code:

class ParentCtrl: ButtonBarPagerTabStripViewController {
    let obj = ChildCtrl()
override func viewDidLoad() {
        super.viewDidLoad()
        obj.someClassMethod()
    }
}

class ChildCtrl: UIViewController, IndicatorInfoProvider {
   struct Storyboard {
    static let segue_id = "segue_id"
}
   public func someClassMethod(){
        self.performSegue(withIdentifier: Storyboard.segue_id, sender: self)
    }

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if(segue.identifier == Storyboard.segue_id){
            let destino = segue.destination as! NewViewController
            destino.seleccion = seleccion
        }
    }
}

But I get the following error:

has no segue with identifier 'segue_id'' But Yeah, I have that identifier because if I copy:

self.performSegue(withIdentifier: Storyboard.segue_id, sender: self)

in the viewDidload method the NewViewController is presented

How can I solve that?

Israel
  • 121
  • 10

2 Answers2

0

Specify the Storyboard Segue identifier, i.e.

enter image description here

And use this id while writing the code to perform segue, i.e.

self.performSegue(withIdentifier: "segue_id", sender: self)

Edit:

Create obj from storyboard, i.e.

let obj = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ChildCtrl") as! ChildCtrl
PGDev
  • 23,751
  • 6
  • 34
  • 88
  • Yes I did that, when I call this: self.performSegue(withIdentifier: Storyboard.segue_id, sender: self) in my viewDidLoad I don't get error, but When I call from my parentview I get the error – Israel Oct 29 '18 at 07:12
  • Is there any segue connecting from `ParentCtrl` to `NewViewController`. – PGDev Oct 29 '18 at 07:14
  • What is the class of of the controller in storyboard? Is it `ChildCtrl` or `ParentCtrl`? – PGDev Oct 29 '18 at 07:16
  • ChildCtrl, I use XLPagerTabStrip lib Where I set the ParentCtrl and that has the ChildCtrl, this is the scene I am trying to access a method from my childCtrl, see my code – Israel Oct 29 '18 at 07:20
  • Is `ChildCtrl` embedded in `ParentCtrl`? – PGDev Oct 29 '18 at 07:22
  • yes, like this override func viewControllers(for pagerTabStripController: PagerTabStripViewController) -> [UIViewController] { var child: UIViewController child = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ChildCtrl") return [child] } – Israel Oct 29 '18 at 07:24
  • I get this : whose view is not in the window hierarchy! in my self.performSegue(withIdentifier: Storyboard.segue_id, sender: self) in my someclassmethod – Israel Oct 29 '18 at 07:28
0

You are simply creating an instance of ChildCtrl rather than using the instance that came from the storyboard. As a result, that instance doesn't know anything about any storyboard file and therefore has no context to perform a segue.

You need to perform the segue from the instance of ChildCtrl that is on screen.

Paulw11
  • 108,386
  • 14
  • 159
  • 186
  • I get this : whose view is not in the window hierarchy! – Israel Oct 29 '18 at 07:36
  • 1
    Don’t call methods on child view controllers in `viewDidLoad`. Call in `viewDidAppear` – Paulw11 Oct 29 '18 at 07:37
  • I am trying to call this someClassMethod() that is out from viewdidload or viewdidappear method – Israel Oct 29 '18 at 07:39
  • You need to add more detail to your question. Show your storyboard and how the various view controllers are created and exactly how you are calling the various functions – Paulw11 Oct 29 '18 at 07:40
  • When I copy this line self.performSegue(withIdentifier: Storyboard.segue_id, sender: self) in my childctrl viedidload() the newviewctrl is show but I want to access from my parent view there I got the error – Israel Oct 29 '18 at 07:42
  • Have you tried it in `viewDidAppear()` because that's the point where all the views have completely loaded ! – Shubham Bakshi Oct 29 '18 at 09:01