Muhammad Murtaza
Muhammad Murtaza

Reputation: 59

How to navigate and pass data to another view controller without use of segue in swift

Hy, I am new to swift. I have on question. I have one view controller in which have a textbox and button and similarly I have second view controller which have a textbox and button. I want to do that whenever I pressed button of first view controller The second view controller appear and data of textbox of first view controller will pass to the textbox of second view controller but without the use of segue. Please help me.

Upvotes: 1

Views: 1590

Answers (4)

user11722374
user11722374

Reputation:

In first VC ,

   let storyboard = UIStoryboard(name: "Main", bundle: nil)

    var vc = storyboard.instantiateViewController(withIdentifier:"SecondViewController") as! SecondViewController

    vc.detail =  [name,age,sal]

   self.present(vc, animated: true, completion: nil)

In second VC ,

var detail:[String]?

Upvotes: -1

Ramesh_T
Ramesh_T

Reputation: 1261

In FirstViewController.swift

@IBAction weak var textFiled: UITextField!

@IBAction func sendDatatoNextVC(sender: UIButton) {
let mainStoryboard = UIStoryboard(name: "Storyboard", bundle: NSBundle.mainBundle())
    let vc : SecondViewController = mainStoryboard.instantiateViewControllerWithIdentifier("SecondViewControllerSBID”) as SecondViewController
vc. recevedString  = textFiled.text
    self.presentViewController(vc, animated: true, completion: nil)
}

In SecondViewController.swift

var recevedString: String =  “”

@IBAction weak var textFiled2: UITextField!

override func viewDidLoad() {
super.viewDidLoad()
textFiled2.text = recevedString

}

Upvotes: 2

Sahil
Sahil

Reputation: 9226

try this: if you creating custom view controller.

var storyboard = UIStoryboard(name: "IDEInterface", bundle: nil)
var controller = storyboard.instantiateViewControllerWithIdentifier("IDENavController") as! MyCustomViewController

controller.exercisedPassed = "Ex1"

self.presentViewController(controller, animated: true, completion: nil)

Upvotes: 0

Dharmesh Kheni
Dharmesh Kheni

Reputation: 71854

You can use instantiateViewControllerWithIdentifier if you don't want to use segue.

Consider below code:

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let someViewConroller = storyboard.instantiateViewControllerWithIdentifier("someViewControllerID") as! SomeViewController
someViewConroller.someObject = yourObject  //here you can assign object to SomeViewController
self.presentViewController(someViewConroller, animated: true, completion: nil)

Upvotes: 0

Related Questions