在同一文件中的视图控制器之间在 Swift 中传递数据 [英] Passing Data in Swift Between View Controllers in Same File

查看:37
本文介绍了在同一文件中的视图控制器之间在 Swift 中传递数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当它们位于同一个 .swift 文件中时,为什么我不能将数据从一个视图控制器类传递到另一个视图控制器类?

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

为了理解传递数据,我已经完成了下面链接的两个教程,并且都让您为第二个视图控制器制作了另一个 .swift 文件.当我完全遵循它们时,它起作用了.当我做的一切都一样但只是将 class SecondViewController 放在与 class FirstViewController 相同的 .swift 文件中时,它不起作用.为什么?

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?

教程:

如何从 YouTube 上的视图控制器 (Swift : Xcode) 传递数据

CodingExplorer 上的 Swift 视图控制器之间的 Segue

这有效:

//  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       
    }
}

这不会:

//  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. 您是否正确设置了视图的类?带有 textField 的视图(可能还有 ViewTwo 的按钮)应设置为 ViewController,而带有标签的第二个视图应设置为 ViewTwo>.

  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.

检查标签/文本字段之间的连接.他们是否正确连接到您的班级?

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

这是我用的代码,应该是一模一样的:

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

导入 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
    }
}

这篇关于在同一文件中的视图控制器之间在 Swift 中传递数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆