简体   繁体   English

在其他ViewController中访问变量Swift

[英]Accessing variables in other ViewControllers Swift

I have a user login to through a welcome screen in my app. 我有一个用户通过我的应用程序中的欢迎屏幕登录。 Now I want that username to pass on to the other view controllers I have in the rest of the app, however i cannot figure out how. 现在,我希望该用户名传递给我在该应用程序其余部分中拥有的其他视图控制器,但是我不知道该怎么做。 In old C i would have done an #import viewcontroller but that is not working. 在旧的C语言中,我会执行#import viewcontroller,但是那没有用。

I have one LoginVC where I set the username from a regular field 我有一个LoginVC,可从常规字段中设置用户名

class LoginVC: UIViewController,UITextFieldDelegate {
    @IBOutlet var txtUsername : UITextField!

now I want to show that txtUsername variable in my ActionVC as a regular var preferably in a function - however I have no idea how? 现在,我想将ActionVC中的txtUsername变量显示为常规var,最好是在函数中显示-但是我不知道如何?

func accessOtherViewController(){
    var userName = LoginVC(txtUsername)
}
 var myCustomViewController: SomeViewController = SomeViewController(nibName: nil, bundle: nil)
    var getThatValue = myTableView.someVariable

You can create a public var to access variables in other view controllers. 您可以创建一个公共变量来访问其他视图控制器中的变量。

At the top of your viewcontroller class add public var name = "saucepan"; 在viewcontroller类的顶部添加public var name = "saucepan";

You should access this var in other view controllers if you only type the name of the var it will turn green (depending on your color scheme). 如果您仅键入将变为绿色的变量名称(取决于您的配色方案),则应在其他视图控制器中访问此变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM