5

Is there a way to segue to the same view controller in Swift? There is an answer for this in Objective-C but I can't convert it to Swift

UPDATE: I was able to push to the same viewcontroller but it immediately errors because it doesn't recognize the objects that were created on the storyboard

let aViewController:myVC = myVC()
self.navigationController!.pushViewController(aViewController, animated:false)

On the Load of the new(same) view controller it errors on the first line:

myText.editable = false;
Cœur
  • 37,241
  • 25
  • 195
  • 267
  • try self.viewDidLoad(), it automatically relaod the same viewcontroller – Anbu.Karthik Oct 01 '15 at 17:35
  • @Anbu.Karthik Is their a way I can pass parameters when calling the load again? I initially load this view using prepareforsegue and I pass over parameters –  Oct 01 '15 at 17:41
  • if you use this concept the total viewcontroller allocated the memory, in here use NSUserDefault, else use local DB value – Anbu.Karthik Oct 01 '15 at 17:45
  • @Anbu.Karthik I noticed if I use this method, my memory usage continues to double after each time viewdidload is called. Then the memory drops to normal usage after opening a different viewcontroller –  Oct 01 '15 at 17:48
  • can @DevTonio can you remove the UPvote for my answer – Anbu.Karthik Oct 01 '15 at 18:37

3 Answers3

8

Set an identifier for your UIViewController in the storyboard. Then instantiate it using that my identifier:

let storyboard = UIStoryboard(name: "MyStoryboardName", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "someViewController")

If you want to set variables, make sure to cast vc. This example will help you know how many levels deep you are on the same controller:

let vc = storyboard.instantiateViewController(withIdentifier: "rootViewController") as! MyRootViewController
vc.level = self.level + 1
Ethan Allen
  • 14,425
  • 24
  • 101
  • 194
Ishan Handa
  • 2,271
  • 20
  • 25
6

I don't do much in swift, but looking at that answer, using self.navigationController?.pushViewController(viewController, animated:true) should work.

Raj Joshi
  • 2,669
  • 2
  • 30
  • 37
Bill L
  • 2,576
  • 4
  • 28
  • 55
  • doesn't work got this error: *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Pushing the same view controller instance more than once is not supported –  Oct 01 '15 at 17:14
  • You can't push the same instance (ie `self`) - you need to instantiate a new Instance from your storyboard – Paulw11 Oct 01 '15 at 17:18
  • @Paulw11 thanks, I got it to push to the same view controller but it immediately errors because it doesn't recognize the objects that were originally created on the storyboard. Says there nil? for example I'm setting a textview.editable to false and it errors due to optional nil –  Oct 01 '15 at 17:31
  • How are you creating the new instance? – Paulw11 Oct 01 '15 at 18:05
  • Thanks, `self.navigationController.pushViewController(vc, animated: true)` is exactly what did it for me after instantiating the view controller. – Andy Rice Jun 29 '16 at 18:28
0

First you connect your button for reloading with ViewController and give segue identifier a name (for instance "yourIdentifier")

Then you need to use:

override func prepare(for segue: UIStoryboardSegue, sender: Any?){
if segue.identifier == "yourIdentifier"{
self.viewDidLoad()
}

This should help.

Shayki Abramczyk
  • 36,824
  • 16
  • 89
  • 114