-4

So, I have Navigation Controller. there are segue from Root View Controller to other View Controller.

When I want to get access to other View Controller I override prepareForSegue method and use destinationViewController property.

But that's not ok for me. All my stuff in prepareForSegue will be execute every time when segue is called, but I don't want it. Secondly, it destroys logic of my code: after performSegueWithIdentifier(actually before) execution jumps to other place in code.

It would be great if I can get access to other View Controller like I did it with Root ViewController - by keyword self, for example.

That's code example to make my question more clearer:

func startWorking() {
   /*here we made some stuff for current VC
...
...
*/

   //next we go to new View Controller
   performSegueWithIdentifier("newVC", sender: nil)

   //then all actions that I want to do begin at another method - prepareForSegue
   //But I want get access to View Controller that user sees now!
   //For example present some view:

   let someView = UIView(frame: someFrame)
   /*question subject*/.view.addSubview(somView)
    }

/question subject/ - is the current ViewController that I have presented by segue and point of my question.

Sergey Gamayunov
  • 658
  • 7
  • 15
  • 1
    *"All my stuff in prepareForSegue will be execute every time when segue is called, but I don't want it"* - then don't put it there... I have trouble understanding what exactly your problem is, can show some code demonstrating what your problem is? – luk2302 May 16 '16 at 13:40
  • 1
    A navigation controller has three methods to access items on the navigation stack. Did you try any of them to see if they work for you? – Desdenova May 16 '16 at 13:46
  • Yes! That's my point! I present new Controller by Segue, does it mean that I have to do all stuff that I want to do with new Controller in prepareForSegue method? – Sergey Gamayunov May 16 '16 at 13:56
  • Possible duplicate of [Get top most UIViewController](http://stackoverflow.com/questions/26667009/get-top-most-uiviewcontroller) – Alessandro Ornano May 16 '16 at 14:08
  • @lu2302 Now question has a code sample as you ask so... thanks in advance! – Sergey Gamayunov May 16 '16 at 14:26
  • @sergey-gamayunov : Did the updated answer worked for you buddy ??? – Sandeep Bhandari May 17 '16 at 09:02
  • @SandeepBhandari It surely works, and your method is very helpful. But... Nevertheless, there is still broken sequence of statements: after `performSegueWithIdentifier` execution still jumps into `navigationController:didShowViewController :animated:` and continues live there. So this `prepareForSegue` did, so this your method is doing. – Sergey Gamayunov May 17 '16 at 10:31

1 Answers1

8

Sergey Gamayunov,

You can always access the top mostViewController in navigation stack using,

let viewCOntroller = self.navigationController?.topViewController

EDIT

I believe if you cant get your logic around the prepareForSegue or self.navigationController?.topViewController you must take a look into your design pattern :)

That being said I understand all you want to do is to access the ViewController after performSegue without using prepareForSegue, you can use this code

func navigationController(navigationController: UINavigationController, didShowViewController viewController: UIViewController, animated: Bool) {
        if viewController is YourDestinationViewControllerClass {
            print("You have access to viewController loaded do whatever you want")
        }
}

The function stated above is a navigation controller delegate :) So you will have to declare your viewController to confirm UINavigationControllerDelegate. like

class ViewController: UIViewController,UINavigationControllerDelegate

and in

override func viewDidLoad() {
       super.viewDidLoad()
       self.navigationController?.delegate = self
}

Thats it you are good to go :) Happy coding buddy :)

Sandeep Bhandari
  • 19,999
  • 5
  • 45
  • 78
  • I tried to use `topViewController` property: `performSegueWithIdentifier("NewVCSegue", sender: self) let newVC = self.navigationController?.topViewController` But `newVC` actually is VC where I came from... – Sergey Gamayunov May 17 '16 at 06:16
  • @sergey-gamayunov : I believe you are very much confused :) Lemme help you out :) Prepare for segue is used to prepare the destinationViewController even before its presented or loaded on to navigation stack :) So if you want to configure the viewCOntroller even before its renderred you should make use of segue.destinationViewController in prepare for Segue :) On the other hand assume your view controller is already loaded and is the topViewController in navigation stack you can make use of self.navigationController?.topViewController. This method will give you access to the existing instance – Sandeep Bhandari May 17 '16 at 07:06
  • Thanks for answer! But in this code sample I didn't use `prepareForSegue`, I used `performSegueWithIdentifier` which should load destinationVC on top in navigation stack... – Sergey Gamayunov May 17 '16 at 07:10
  • @sergey-gamayunov : May I know what exactly are you trying to achieve here buddy ??? I probably will be able to write a solution if I understand your problem correctly. Your question is not so clear :( as it is evident from -4 votes :| can you please explain your need ??? – Sandeep Bhandari May 17 '16 at 07:14
  • My question (I have already added code demonstration above) is how to get access to current ViewController that appeared by segue transition. – Sergey Gamayunov May 17 '16 at 07:24
  • @sergey-gamayunov : I have update my answer :) You can see. I hope it will solve your issue :) – Sandeep Bhandari May 17 '16 at 08:06