import UIKit
import Foundation
class First: UIViewController {
@IBOutlet var firstTitle: UILabel!
@IBOutlet var firstTxtField: UITextField!
@IBAction func goTo2nd(sender: AnyObject) {
var second: Second?
second?.secondTitle.text = firstTxtField.text
}
}
import Foundation
import UIKit
class Second: UIViewController {
@IBOutlet var secondTitle: UILabel!
@IBOutlet var secondTextField: UITextField!
@IBAction func goToFirstVC(sender: AnyObject) {
var first: First?
first?.firstTitle.text = secondTextField.text
}
}
为什么我无法将文本字段值分配给出口标签,我在故事板中创建了2个viewControllers,并为每个分配了这个(First.swift,Second.swift)类
答案 0 :(得分:0)
import UIKit
import Foundation
class First: UIViewController {
//下面这个var用作我们将通过在其他swift类中创建该类的对象来访问的属性
var firstTit:String?
@IBOutlet var firstTitle: UILabel!
@IBOutlet var firstTxtField: UITextField!
@IBAction func goTo2nd(sender: AnyObject) {
}
override func viewDidLoad() {
super.viewDidLoad()
firstTitle.text = firstTit
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func prepareForSegue(segue: (UIStoryboardSegue!), sender: AnyObject!) {
if (segue.identifier == "goto2from1")
{
var svc = segue!.destinationViewController as Second;
svc.secondTit = firstTxtField.text
}
}
}
import Foundation
import UIKit
class Second: UIViewController {
var secondTit: String?
@IBOutlet var secondTitle: UILabel!
@IBOutlet var secondTextField: UITextField!
@IBAction func goToFirstVC(sender: AnyObject) {
}
@IBAction func goToThirdVC(sender: AnyObject) {
}
override func viewDidLoad() {
super.viewDidLoad()
secondTitle.text = secondTit
}
override func prepareForSegue(segue: (UIStoryboardSegue!), sender: AnyObject!) {
if (segue.identifier == "goto1from2")
{
var svc = segue!.destinationViewController as First;
svc.firstTit = secondTextField.text
}
else if(segue.identifier == "goto3from2")
{
var svc = segue!.destinationViewController as Third;
svc.thirdTit = secondTextField.text
}
}
}
import Foundation
import UIKit
class Third: UIViewController {
var thirdTit:String?
@IBOutlet var thirdTitle: UILabel!
@IBOutlet var thirdTextField: UITextField!
override func viewDidLoad()
{
super.viewDidLoad()
thirdTitle.text = thirdTit
}
@IBAction func goTofirstFromThird(sender: AnyObject) {
}
override func prepareForSegue(segue: (UIStoryboardSegue!), sender: AnyObject!) {
if (segue.identifier == "goto1from3")
{
var svc = segue!.destinationViewController as First;
svc.firstTit = thirdTextField.text
}
}
}
另一种方式,如果你不想通过第二个ViewController传递数据,但想直接发送到第三个,使用结构
import UIKit
import Foundation
class First: UIViewController {
var firstTit:String?
@IBOutlet var firstTitle: UILabel!
@IBOutlet var firstTxtField: UITextField!
@IBAction func goTo2nd(sender: AnyObject)
{
Third.thirdGlobalVars.thirdTit = firstTxtField.text //this line sends field data to third class' preperty (in fact a struct)
//if u r going through 2nd vc to 3rd, to move from 1st vc to 2nd + if u want to do progammatic initialization use the following line as well
var storyboard = UIStoryboard(name: "Main", bundle: nil)
var vc = storyboard.instantiateViewControllerWithIdentifier("secondVC") as Second
self.presentViewController(vc, animated: true, completion: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
firstTitle.text = firstTit
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
class Third: UIViewController {
struct thirdGlobalVars {
static var thirdTit: String?
}
@IBOutlet var thirdTitle: UILabel!
@IBOutlet var thirdTextField: UITextField!
override func viewDidLoad()
{
super.viewDidLoad()
thirdTitle.text = thirdGlobalVars.thirdTit
}