Passing variables between Storyboards without Segues - Swift

前端 未结 4 885
终归单人心
终归单人心 2020-12-16 06:06

I\'m passing data between two different UIViewControllers located within two different .storyboard files within Xcode. (It\'s quite a large project so I had to break it up i

相关标签:
4条回答
  • 2020-12-16 06:36

    I'm assuming that the viewController that you want to pass the data to is a custom viewController. In that case, use this amended code here:

    var storyboard = UIStoryboard(name: "IDEInterface", bundle: nil)
    var controller = storyboard.instantiateViewControllerWithIdentifier("IDENavController") as! MyCustomViewController
    
    controller.exercisedPassed = "Ex1"
    
    self.presentViewController(controller, animated: true, completion: nil)
    
    0 讨论(0)
  • 2020-12-16 06:42

    your question is not very clear, as far as i have understood

    //1
    //create a variable in "IDENavController"
    //e.g 
    var someVariable = @""
    
    //2
    var controller = storyboard.instantiateViewControllerWithIdentifier("IDENavController") as!       UIViewController
    controller.someVariable = [assign your variable]
    
    //3
    self.presentViewController(controller, animated: true, completion: nil)
    
    0 讨论(0)
  • 2020-12-16 06:45

    For those who are looking for the Swift 3/4 answer:

    let storyboard = UIStoryboard(name: "IDEInterface", bundle: nil)
    let controller = storyboard.instantiateViewController(withIdentifier: "IDENavController") as! CustomViewController
    
    controller.yourVariable = "abc"
    
    self.present(controller, animated: true, completion: nil)
    
    0 讨论(0)
  • 2020-12-16 06:50

    Try this. This worked for me.

    var storyboard = UIStoryboard(name: "IDEInterface", bundle: nil)
    var controller = storyboard.instantiateViewControllerWithIdentifier("IDENavController") as! YourDestinationViewControllername
    // Add your destination view controller name and Identifier
    
    // For example consider that there is an variable xyz in your destination View Controller and you are passing "ABC" values from current viewController.
    
    controller.xyz = "ABC"
    
    self.presentViewController(controller, animated: true, completion: nil)
    

    Hope this will be helpful.

    0 讨论(0)
提交回复
热议问题