简体   繁体   English

我如何访问UITextField的文本值iOS Swift 4

[英]How do i access the text value of a UITextField ios swift 4

I am new to ios programming and this may be me being very stupid (quite likely). 我是ios编程的新手,这可能是我非常愚蠢(很有可能)。

I have two viewcontrollers in my app. 我的应用程序中有两个ViewController。 To view the second viewcontroller i use a seque and then return to the first viewcontroller with the back button on the seconf viewcontroller navigation bar. 要查看第二个ViewController,请使用seque,然后使用seconf viewcontroller导航栏上的“后退”按钮返回到第一个Viewcontroller。 This all works fine. 这一切都很好。

On the second viewcontroller i have a UITextField which is populated following a background notification event with text returned from an external class. 在第二个viewcontroller上,我有一个UITextField,它是在背景通知事件之后填充的,它是从外部类返回的文本。

The first time i seque to the second viewcontroller and trigger the event to populate the UITextField it works correctly. 我第一次选择第二个viewcontroller并触发该事件以填充UITextField,它可以正常工作。

If i then go back to the first viewcontroller and then seque to the second viewcontroller a second time, the UITextField will not update. 如果我然后返回到第一个视图控制器,然后第二次连接到第二个视图控制器,则UITextField将不会更新。

I suspect that each time i seque to the second viewcontroller, i am creating a new instance of that controller and when i try and update the UITextField i am still updating the first instance of the viewcontroller which is not visible. 我怀疑每次我固定到第二个ViewController时,我都会创建该控制器的新实例,并且当我尝试更新UITextField时,我仍在更新第一个不可见的ViewController实例。

It fells as though i should address the UITextField in someway like UITextFied[current viewcontroller].text = "Hello World". 好像我应该以某种方式解决UITextField一样,UITextFied [current viewcontroller] .text =“ Hello World”。

Any help or guidance would be much appreciated. 任何帮助或指导将不胜感激。

Thanks 谢谢

I not sure if I understand your problem but its sound like you define your background notification event in viewDidLoad in first view controller try it in viewDidApper or viewWillApper 我不确定我是否理解您的问题,但听起来像是您在第一个视图控制器的viewDidLoad中定义了背景通知事件,然后在viewDidApperviewWillApper中尝试

ViewDidLoad only call ones on its first load ViewDidLoad仅在第一次加载时调用

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

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