how to send data from one ViewController to anothe

2019-09-06 07:41发布

问题:

I have impleented SWRevealViewController for sideMenu. My project is is Swift and SWRevealViewController is in Objective-c. SWRevealViewController is not initial view Controller.there is one intial View Contoller(1VC) and it has Push segue on SWRevealViewController. another ViewController (2VC) is act as FrontView. I want to pass data from (1VC) to (2VC). but I could not find any way

回答1:

Two ways:

In case of segue

override func prepareForSegue(segue: UIStoryboardSegue!,sender: AnyObject!){         
//make sure that the segue is going to secondViewController
if segue.destinationViewController is secondViewController{
// now set a var that points to that new viewcontroller so you can call the method correctly
let nextController = (segue.destinationViewController as! secondViewController)
nextController.setResultLabel((String(myResult)))
 }
}

Otherwise in swift everything is public by default. Define variables outside the class

import UIKit

var placesArray: NSMutableArray!

class FirstViewController: UIViewController {
//
..
//
}

and then access it

import UIKit

class SecondViewController: UIViewController {
//
placesArray = [1, 2, 3]
//

}