显示viewController并传递数据而无需情节提要和segue编程

时间:2018-07-02 08:34:30

标签: ios swift

我想将firstVC数据传递给secondVC。
但是我不知道如何通过编程。

Class FirstVC: ViewController {

   var firstText: String = "hello"

   func goToSecondVC {
       var vc: SecondVC = SecondVC()
       vc.secondText = firstText
       self.show(vc, sender:nil)
   }

}

class SecondVC: ViewController {

var secondText: String = ""

    override func viewDidLoad() {

        print(secondText)
    }
}

我在堆栈中搜索并找到以下答案。
还有没有故事板,segue,delegate和NSUserDefaults的答案?这个答案使用情节提要。

let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let newViewController = storyBoard.instantiateViewController(withIdentifier: "newViewController") as! NewViewController

newViewController.stringVariable = stringVariable

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

1 个答案:

答案 0 :(得分:0)

在第一个ViewController中,

<?php

namespace App\Models\Admin;

use Illuminate\Database\Eloquent\Model;

class Payment extends Model
{
    protected $table = "payments";
    public $timestamps = false;
    protected $cast = ["payDate"=> "DATE"];
}

在第二个ViewController中,     //只需定义字符串

@IBAction func Some_Button(_ sender: Any) {
    let secndVC = self.storyboard?.instantiateViewController(withIdentifier: 
    "SecondViewControllerID") as! SecondViewController
    let someString = "this is my string" //Whatever You Want to pass.
    secndVC.SomeString=someString
    self.navigationController?.pushViewController(secndVC, animated: true)
}

您将在viewDidLoad中获取数据

var SomeString = ""
//that's it.