如何从 SecondViewController 修改 FirstViewController 中的 UITextField?

发布于 2025-01-03 02:54:40 字数 360 浏览 4 评论 0原文

所以我对 Objective C 和 iOS 编程相当陌生。我一边编程一边学习,有一个问题。我试图用我的 Second 修改我的 FirstViewController 中的 UITextField 。我该怎么做?

我在 FirstViewController 中有一个 UITextField 名称,并且希望能够使用按钮在 SecondViewController 内执行类似 firstViewController.name.text = @"new name"; 的操作值已更改,以便如果我将选项卡切换回 FirstViewController,我会看到已进行更改。

预先感谢您的任何帮助。

So I am fairly new to objective C and iOS programming. I am learning as I program and have a question. I am trying to modify a UITextField in my FirstViewController with my Second. How do I do this?

I have a UITextField name in FirstViewController and would like to be able to do something like firstViewController.name.text = @"new name"; inside of SecondViewController with a button and have the value changed so that if I switch tabs back to the FirstViewController I see the change has been made.

Thanks in advance for any help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无敌元气妹 2025-01-10 02:54:40

1、将FirstViewController设置为SecondViewController的委托;
2、使用NSNotificationCenter通知变更事件。

1、Set the FirstViewController as the delegate of SecondViewController;
2、Use NSNotificationCenter to notify the change event.

向日葵 2025-01-10 02:54:40

很好调用

firstViewController.name.text = @"new name"

您只需要在第二个视图控制器内引用第一个视图控制器。有多种方法可以传递引用。可以通过应用程序委托。

不需要您在第二个视图控制器中引用第一个视图控制器的另一个选项是使用 NSNotificationCenter

然后你可以让你的第二个视图控制器观察一个名为 say 的通知@“namechange”,其中包含新的名称字符串。

当按下该按钮时,您将附加新名称的通知发布到 NSNotificationCenter。所有信息都在上面的链接中,或者只需谷歌 NSNotificationCenter 即可获取更多教程和信息。

Well to call

firstViewController.name.text = @"new name"

You just need a reference to your first view controller inside of your second view controller. There are a number of ways to pass a reference around. Could go through the app delegate.

Another option that doesn't require you to have a reference to the first view controller inside the second view controller is to use NSNotificationCenter

Then you can have your second view controller observe a notification named say @"namechange" which contains the new name string.

When the button is pressed, you post a notification to the NSNotificationCenter with the new name attached. All the info is in the link above, or just google NSNotificationCenter for more tutorials and info.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文