Swift : prepareForSegue with navigation controller

后端 未结 8 838
情书的邮戳
情书的邮戳 2020-12-12 17:46

I am developing an iOS application in Swift.

I want to send data from a view to an other one, using the prepareForSegue function.

However, my target view is

相关标签:
8条回答
  • 2020-12-12 18:20

    Complete answer using optional binding and Swift 3 & 4:

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if let navigationVC = segue.destination as? UINavigationController, let myViewController = navigationVC.topViewController as? MyViewControllerClass {
            myViewController.yourProperty = myProperty
        }
    }
    
    0 讨论(0)
  • 2020-12-12 18:25

    Set the identifier name in the segue arrow property in order to use in the the performeSegue.

    Like this:

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    
        if let vc: ProfileViewController = segue.destination as? ProfileViewController {
    
            //do any setting to the next screen
    
        }
    
    }
    

    and then:

        performSegue(withIdentifier: "yourIdentifierOfViewProfile", sender: indexPath.row)
    

    I hope it helps.

    0 讨论(0)
  • 2020-12-12 18:27

    It's a good idea to skip the check for UINavigationController as there may be multiple segues that use a navigationController and so will go into that check for every segue that uses a navigationController. A better way is to check the first viewController of the children and cast it as the viewController you are looking for.

    if let destVC = segue.destination.children.first as? MyViewController {
        destVC.hideBottomBar = true
    }
    
    0 讨论(0)
  • 2020-12-12 18:30
    1. Prepare the segue in the SendViewController

      override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
          if segue.identifier == "segueShowNavigation" {
              if let destVC = segue.destination as? UINavigationController,
                  let targetController = destVC.topViewController as? ReceiveViewController {
                  targetController.data = "hello from ReceiveVC !"
              }
          }
      }
      
    2. Edit the identifier segue to "showNavigationController"

    1. In your ReceiveViewController add

    this

    var data : String = ""
    
    override func viewDidLoad() {
        super.viewDidLoad()
        print("data from ReceiveViewController is \(data)")
    }
    

    Of course you can send any other type of data (int, Bool, JSON ...)

    0 讨论(0)
  • 2020-12-12 18:34

    A one liner in Swift 3:

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
      if let vc = segue.destination.childViewControllers[0] as? FooController {
        vc.variable = localvariable
      }
    }
    
    0 讨论(0)
  • 2020-12-12 18:37

    Here is the answer for Swift 3:

    let svc = segue.destination as? UINavigationController
    
    let controller: MyController = svc?.topViewController as! MyController
    controller.myProperty = "Hi there"
    
    0 讨论(0)
提交回复
热议问题