如何从不同视图访问一个视图上的对象?

发布于 2025-01-02 09:58:32 字数 739 浏览 0 评论 0原文

我有一个带有两个选项卡的选项卡栏控制器:

  • tabBar1View 带有一个名为“openDifferentView”的按钮和一个文本字段 “myvalue”,带有文本“test”
  • tabBar2View,带有名为的文本字段 “txtInTab2”

当我点击 openDifferentView 时,我希望 tabBar2View 显示,并将值“test”(来自 tabBar1View.myvalue.text)传递到 txtInTab2 (在 tabBar2View 中)。

我使用 [self.tabBarController setSelectedIndex:1] 显示视图,但我不确定如何从此视图中设置其文本字段的值。

我认为这是可能的:

UIViewController *tab2;
tab2 = [self.tabBarController.viewControllers objectAtIndex:1];
tab2.txtInTab2.text = "something"; //doesn't work
tab2.show; //don't know how to this

编辑:我已经为 tabBar2View 中的文本字段添加了 IBOutlet @property/@synthesize,并在 tabBar1View 中添加了 #import "tabBar2View.h"。

I have a tab bar controller with two tabs:

  • tabBar1View with a button named "openDifferentView" and a textfield
    "myvalue" with text "test"
  • tabBar2View with a textfield named
    "txtInTab2"

When I tap on openDifferentView i want tabBar2View to show, and pass the value "test" (from tabBar1View.myvalue.text) to txtInTab2 (in tabBar2View).

I get the view to show up using [self.tabBarController setSelectedIndex:1] but I'm unsure how I can set the value of its textfield, from this view.

I thought this would be possible:

UIViewController *tab2;
tab2 = [self.tabBarController.viewControllers objectAtIndex:1];
tab2.txtInTab2.text = "something"; //doesn't work
tab2.show; //don't know how to this

Edit: I have already added IBOutlet @property/@synthesize for the textfield in tabBar2View, and #import "tabBar2View.h" in tabBar1View.

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

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

发布评论

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

评论(2

安稳善良 2025-01-09 09:58:32

UIViewController 没有名为 txtInTab2 的属性。 txtInTab2 是您添加到您自己的 UIViewController 子类(您称为 TabBar2View)的属性。

代码基本上是正确的,但是您需要将 tab2 定义为它实际上是什么类型的视图控制器。从下面的评论来看,它实际上是一个导航控制器,因此您需要执行以下操作:

#import "MyViewController.h"

UInavigationController *tab2;
tab2 = [self.tabBarController.viewControllers objectAtIndex:1];

//now lets get the frontmost view controller in the navigation controller
//which will (hopefully) be your custom view controller class
TabBar2View *viewController = (TabBar2View *)tab2.topViewController;

//now before we set the label, we need to make sure that the
//view controller's view has actually been loaded from its nib
//file. calling its view property forces it to load
[viewController view];

//now we can set the label
viewController.txtInTab2.text = @"something"; //this will work now

然后要实际显示它,您需要通过选项卡栏控制器执行此操作,如下所示:

[self.tabBarController setSelectedIndex:1]; //show the second tab

UIViewController doesn't have a property called txtInTab2. txtInTab2 is a property you've added to your own UIViewController subclass (which you've called TabBar2View).

The code is basically right, but you need to define tab2 as being whatever type of view controller it actually is. From your comments below, it's actually a navigation controller, so you'll need to do this:

#import "MyViewController.h"

UInavigationController *tab2;
tab2 = [self.tabBarController.viewControllers objectAtIndex:1];

//now lets get the frontmost view controller in the navigation controller
//which will (hopefully) be your custom view controller class
TabBar2View *viewController = (TabBar2View *)tab2.topViewController;

//now before we set the label, we need to make sure that the
//view controller's view has actually been loaded from its nib
//file. calling its view property forces it to load
[viewController view];

//now we can set the label
viewController.txtInTab2.text = @"something"; //this will work now

Then to actually show it, you need to do this via the tab bar controller, like this:

[self.tabBarController setSelectedIndex:1]; //show the second tab
要走干脆点 2025-01-09 09:58:32

您需要使用代表。我认为 这个会给你一个关于如何开始的很好的帮助

You need to use delegates. I think this will give you a good help about how to get started

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