如何导航并将数据传递给另一个视图控制器而不使用swift中的segue

时间:2016-01-21 06:32:51

标签: ios swift

Hy,我是swift的新手。我有疑问。我有一个视图控制器,其中有一个文本框和按钮,同样我有第二个视图控制器,它有一个文本框和按钮。每当我按下第一个视图控制器的按钮时,我想这样做第二个视图控制器出现,第一个视图控制器的文本框数据将传递到第二个视图控制器的文本框但不使用segue。 请帮帮我。

4 个答案:

答案 0 :(得分:2)

在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)
}

在SecondViewController.swift中

var recevedString: String =  “”

@IBAction weak var textFiled2: UITextField!

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

}

答案 1 :(得分:0)

如果您不想使用instantiateViewControllerWithIdentifier,则可以使用segue

请考虑以下代码:

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)

答案 2 :(得分:0)

试试这个: 如果你创建自定义视图控制器。

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

controller.exercisedPassed = "Ex1"

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

答案 3 :(得分:-1)

在第一个VC中,

for (const obj of jsonresponse) {

  /* 
  Obtain target document ref from path that is obj id, relative to collection type 
  */
  db.ref(`users/${ obj.id }`).set(obj);
}

在第二个VC中,

var detail:[String]?