0

I have a splashscreen which is shown for 5 seconds. After this time I call another ViewController(VC_Products) programmatically with a seque and want to pass parameters, but my ViewController does not receive anything.

override func viewDidLoad() {
    super.viewDidLoad()
    //here we set a timer to start after a given amount of seconds the ProductViewController
    _ = Timer.scheduledTimer(timeInterval: 5.0, target: self, selector: #selector(timeToMoveOn), userInfo: nil, repeats: false)
}


@objc func timeToMoveOn() {

    //change root navigation controller
    let appDelegate = UIApplication.shared.delegate as! AppDelegate
    appDelegate.changeRootViewControllerToSWRevealViewController()

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

}


override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if (segue.identifier == "splashscreen_to_products") {

        if let vc: VC_Products = segue.destination as? VC_Products {
               vc.titleremote = "variableToPass"
        }

    }
}  

In the VC_Products:

I have following:

var titleremote:String = ""

and it does not change to "variableToPass". Nothing happens! Why?

Maybe my seque is wrong? enter image description here

UPDATE:

Seemed that changing rootview controller to SWRevealViewController was the cause why I could not pass parameters. I removed the SWrevealViewController and it worked. Somehow, I need the SWRevealViewController to have a left menu, but then I am not able to pass data. I wanted to load images from remote URL in splashscreen and pass them then to a VC_Products where the images are presented. This is not possible. I searched for two days finding a way to pass the image thorugh the SWRevealViewController, but without success. I will now go a different way and save the loaded image in the splashscreen to temporary folder and just load the images from there in the VC_Products. That is what I hate about iOS coding. Storyboard and the whole navigation between controllers is horrible as the layout constraints and debugging is in iOS. In Android it just works without any problems.

Adam Ri
  • 347
  • 2
  • 15
  • Add `print(type(of: segue.destination))` at the start of `prepare(for:sender:)` and tell us what it prints. – vacawama Dec 20 '18 at 17:49
  • @vacawama: print(type(of: segue.destination)): UINavigationController – Adam Ri Dec 20 '18 at 17:52
  • 1
    Ah, thought so. See: https://stackoverflow.com/q/26951195/1630618 – vacawama Dec 20 '18 at 17:52
  • Added solution from link and still getting nil for titleremote! – Adam Ri Dec 20 '18 at 18:01
  • 1
    Add `print(type(of: navController.topViewController))`. You should see `VC_Products`. – vacawama Dec 20 '18 at 18:04
  • Result: Optional The more: Warning: Attempt to present on whose view is not in the window hierarchy! – Adam Ri Dec 20 '18 at 18:08
  • You need to set the class of your UIViewController in the Storyboard to VC_Products. – vacawama Dec 20 '18 at 18:09
  • It is set to VC_Products. Maybe it is due to change of RootViewController. Take a look at the function timeToMoveOn(). First two lines lead to appdelegate: func changeRootViewControllerToSWRevealViewController () { let storyboard = UIStoryboard(name: "Main", bundle: nil) let controller = storyboard.instantiateViewController(withIdentifier: "RevealViewController") if let window = self.window{ window.rootViewController = controller } } – Adam Ri Dec 20 '18 at 18:12
  • Why are you doing that? I thought you were trying to segue from the splash screen to VC_Products. The swap moves your current viewController out of the window hierarchy, so you've pulled the rug out under your own feet. – vacawama Dec 20 '18 at 18:16
  • Not based on my shit. Take a look at the pic. Above the NavigationController there is the SWRevealViewController. It is needed to have a kind of left menu and being able to swipe so the left menu is shown to user. – Adam Ri Dec 20 '18 at 18:19
  • Thanks Bro! I removed the controller appDelegate.changeRootViewControllerToSWRevealViewController() and now it works, but I have somehow find another solution now to get left menu to work! Thanks for your effort!!! Great guy! – Adam Ri Dec 20 '18 at 18:24
  • OK. Good luck with the left menu. – vacawama Dec 20 '18 at 18:25
  • If problem is solved, give answer or delete question please. – matt Dec 20 '18 at 18:35
  • instead of changing root view controller use a navigation controller, once the new VC opens up you can pop the starting VC so its not in the stack – Justin Miller Dec 20 '18 at 19:01
  • Thanks, but I don't get it. It only works without left menu. Cant get left menu to work otherwise. Thats what I hate about swift and iOS coding. Storyboard, Layout constraints, debuggin and navigation between VCs is horrible. I prefer Android way more. No problems with navigation, autolayout or debugging. – Adam Ri Dec 27 '18 at 12:39

0 Answers0