尝试使用不同视图控制器上的 UITextField 内容更新 UILabel

发布于 2024-11-19 17:24:20 字数 630 浏览 2 评论 0原文

我有两个视图控制器,一个是MainViewController,另一个是SetupViewController。当在 SetupViewController 中按下按钮时,我希望 MainViewController 上的 UILabel 将文本设置为 SetupViewController 中 UITextField 的内容。

在SetupViewController中,我在IBAction中有这个:

- (IBAction)donePressed:(id)sender {  
   MainViewController *mvc = [[MainViewController alloc] init]; 
  [mvc.testLabelOnMVC setText:testTextFieldOnSVC.text];
  [release mvc];
}

testLabelOnMVC(和testTextFieldOnSCV,以及相应的术语)是

@property (nonatomic, retain) UILabel *testLabelOnMVC;

并且也是合成的。

每次我尝试,都行不通。什么都没有发生,什么也没有改变。我没有任何错误或警告。有人可以帮我吗?

I have two view controllers, one is MainViewController, the other is SetupViewController. I want a UILabel on MainViewController to set the text to the contents of a UITextField from the SetupViewController when a button is pressed in the SetupViewController.

In SetupViewController, I have this in the IBAction:

- (IBAction)donePressed:(id)sender {  
   MainViewController *mvc = [[MainViewController alloc] init]; 
  [mvc.testLabelOnMVC setText:testTextFieldOnSVC.text];
  [release mvc];
}

testLabelOnMVC (and testTextFieldOnSCV, with respective terms) is

@property (nonatomic, retain) UILabel *testLabelOnMVC;

and is also synthesized.

Every time I try, it doesn't work. Nothing happens, nothing changes. I have no errors or warnings. Can anyone help me out?

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

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

发布评论

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

评论(5

木有鱼丸 2024-11-26 17:24:21

在引用 MainViewController 的 view 属性(强制 viewDidLoad 执行)之前,MainViewController 的视图并不存在。在尝试修改 MainViewController 中的任何 UI 对象之前,必须引用视图(或以其他方式强制构造视图)。

The view of your MainViewController does not exist until you reference the MainViewController's view property (which forces viewDidLoad to execute). You must reference the view (or otherwise force the view to be constructed) before you attempt to modify any UI objects in the MainViewController.

指尖微凉心微凉 2024-11-26 17:24:21

当您按下按钮时,您将分配一个新的 MainViewController,然后您将在这个新控制器上设置标签的文本,而不是在您的应用程序显示的 MainViewController 上。

要解决此问题,请创建指向原始 MainViewController 的 IBOutlet 或 iVar,并在其上设置文本。

You are allocating a new MainViewController when you press the button, then you are setting the text of the label on this new controller, not on the MainViewController that your app is showing.

To fix this, create either and IBOutlet or iVar that points to the original MainViewController and set the text on that instead.

笑咖 2024-11-26 17:24:21

最简单的方法是在主视图控制器中创建一个 @property 并在其中写入文本。然后在第二个MVC的viewDidLoad中读取它即可。

Easiest way is to create a @property in the main view controller and write the text in there. Then just read it in the second MVC's viewDidLoad.

つ可否回来 2024-11-26 17:24:21

MainViewController 唯一应该担心的是它拥有的视图;它不应该尝试访问由SetupViewController 管理的视图层次结构。同样,SetupViewController 不应直接修改 MainViewController 视图图中的视图。

执行您所要求的操作的正确方法是让两个控制器直接或通过数据模型相互通信。例如,假设您的 MainViewController 实例化了 SetupViewController。如果是这种情况,mvc 很自然地将自己设置为 svc 的委托,以便 svc 向其发送如下消息: -setupController:didUpdateTestStringTo:。 MainViewController 的该方法的实现可以将新的测试字符串保存在某处并更新其 testLabel 字段。

另一个例子:MainViewController实例化SetupViewController。 SetupViewController 包含一个字段,用户可以在其中输入测试字符串的新值。在退出之前,SetupViewController 将该字段的内容写入 NSUserDefaults 或其他一些常见的数据存储中。当控制权返回到 MainViewController 时,该对象会读取共享数据并根据需要更新自身,包括为 testLabel 设置新值。

同一主题还有其他变体,但这里的共同点是视图控制器都不必直接访问它不拥有的视图。

The only views that MainViewController should worry about are the ones that it owns; it shouldn't be trying to access the view hierarchy managed by SetupViewController. Likewise, SetupViewController should not directly modify views in MainViewController's view graph.

The right way to do what you're asking is for the two controllers to talk to each other, either directly or via the data model. For example, let's say that your MainViewController instantiates SetupViewController. If that's the case, it'd be natural for mvc to set itself as svc's delegate, so that svc sends it a messages like -setupController:didUpdateTestStringTo:. MainViewController's implementation of that method could then save the new test string somewhere and update it's testLabel field.

Another example: MainViewController instantiates SetupViewController. SetupViewController contains a field where the user can enter a new value for the test string. Before exiting, SetupViewController writes the contents of that field into NSUserDefaults or some other common data storage. When control returns to MainViewController, that object reads the shared data and updates itself as necessary, including setting the new value for testLabel.

There are other variations on the same theme, but the common thread here is that neither view controller has to directly access views that it doesn't own.

撕心裂肺的伤痛 2024-11-26 17:24:21

如果视图已加载,您可以更改标签的文本。如果您使用导航控制器,则不要初始化视图控制器,而是从视图堆栈中检索它。
我不知道你的 viewController 是否已经加载。

You can change the text of the label if the view is already loaded. Instead of initializing the viewcontroller, retrieve it from the view stack if you are using navigation controller.
I dont know if your viewController is already loaded or not.

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