Passing Data Between View Controllers Without Segue

Passing Data between View Controllers without segue

If its a small data like some string or variable you can use UserDefault or pass data using that class variable like vc2.data = data
How to use UserDefaults How to use UserDefaults in swift?

If its more like table or number of user list you can use plist store in your bundle at can retrieve from any view controller
using Plist How do I get a plist as a Dictionary in Swift?

if two controller are some how connected you can use delegates.
delegats:- Delegates in swift?

You can also use notifications if you are not sure when data will be available/ or send base on some action, or send if something is trigged.

Pass data using Notifications How to pass data using NotificationCentre in swift 3.0 and NSNotificationCenter in swift 2.0?

Passing Data with without Segue from Container View to MainVC

Delegate pattern should help you. First of all you need the delegate protocol:

protocol SelectSoundVCDelegate {
func didSelectTrackNumber(_ trackNumber: Int)
}

with func that accept the trackNumber value. Next, you need to create delegate property in SelectSoundVC:

class SelectSoundVC: UIViewController {

weak var delegate: SelectSoundVCDelegate?
var trackNumber: Int!

@IBAction func winterSoundBut(_ sender: UIButton) {
trackNumber = 1
delegate?.didSelectTrackNumber(trackNumber)
}

}

that will be call the didSelectTrackNumber in @IBAction. Note that delegate property should be weak to avoid reference cycles. The last step, in MainVC you should set delegate property of SelectSoundVC:

selectSoundVC.delegate = self  

This part is little bit tricky, because you need the instance of SelectSoundVC to set the delegate. You can set it in prepareFoSegue method, for example:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let viewController = segue.destination as? SelectSoundVC, segue.identifier == "SelectSoundVC" {
viewController.delegate = self
}
}

Pass data between view controllers WITHOUT segues

You can create a Segue not bounded to a button by ctrl+drag from the first controller to the second one (don't forget to give this segue and identifier).

Next In the IBAction of the button (set via Interface Builder or via addTarget:self action:forControlEvents: ) you can call the [self performSegueWithIdentifier:@"YourSegueIdentifier" sender:button];

You can pass the data to the second controller, as usual, in - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender

how to send data between view controllers without using segue in IOS 9

You can instantiate the destination view controller using storyboard instance and pass the data further :

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "TestViewController") as! TestViewController
controller.variableX = self.variableX
self.present(controller, animated: true, completion: nil)

Passing data to ViewController in Swift without segue

What you do with the instantiateViewController(withIdentifier:) in the table view delegate is creating a new instance of ChangeBalanceViewController. To access the actual parent of the second view controller you need to use self.parent in the second view controller.

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if let firstVC = self.parent as? ChangeBalanceViewController {
firstVC.changeBalanceTextField.text = "TEST"
}
}


Related Topics



Leave a reply



Submit