Passing Data in Swift Between View Controllers in Same File

非 Y 不嫁゛ 提交于 2019-12-13 02:06:56

问题


Why can't I pass data from one view controller class to another view controller class when they are in the same .swift file?

I've done both of the tutorials linked bellow in an effort to understand passing data, and both have you make another .swift file for you second view controller. When I followed them exactly it works. When I do everything the same but just put the class SecondViewController in the same .swift file as the class FirstViewController it doesn't work. Why?

I'm building an application with three view controllers and I'd rather just keep all the code in one file if I can. Is this why it's not working or is it just bad form?

Tutorials:

How to Pass Data from View Controller (Swift : Xcode) on YouTube

Segue Between Swift View Controllers on CodingExplorer

This works:

//  ViewController.swift
import UIKit
class ViewController: UIViewController {
    @IBOutlet var TextField: UITextField!    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        var DestViewController: ViewTwo = segue.destinationViewController as ViewTwo        
        DestViewController.LabelText = TextField.text        
    }
}

//  ViewTwo.swift
import UIKit
    class ViewTwo: UIViewController {
    @IBOutlet var Label: UILabel!    
    var LabelText = String()    
    override func viewDidLoad() {        
        Label.text = LabelText       
    }
}

This does not:

//  ViewController.swift
import UIKit

class ViewController: UIViewController {
    @IBOutlet var TextField: UITextField!    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        var DestViewController: ViewTwo = segue.destinationViewController as ViewTwo        
        DestViewController.LabelText = TextField.text        
    }
}

class ViewTwo: UIViewController {
    @IBOutlet var Label: UILabel!    
    var LabelText = String()    
    override func viewDidLoad() {        
        Label.text = LabelText       
    }
}

回答1:


Working fine for me.

  1. Have you set the classes for your views properly? The view with your textField (and probably a button to ViewTwo) should be set to ViewController and your second view with the label should be set to ViewTwo.

  2. Check the connections between your label/textField. Are they properly connected to your class?

  3. This is the code I used, which should be exactly the same:

import UIKit

class ViewController: UIViewController {

    @IBOutlet var TextField: UITextField!

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        var DestViewController = segue.destinationViewController as! ViewTwo
        DestViewController.LabelText = TextField.text
    }
}

class ViewTwo: UIViewController {

    @IBOutlet var Label: UILabel!

    var LabelText = String()

    override func viewDidLoad() {
        Label.text = LabelText
    }
}


来源:https://stackoverflow.com/questions/29575020/passing-data-in-swift-between-view-controllers-in-same-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!