如何从不同视图访问一个视图上的对象?
我有一个带有两个选项卡的选项卡栏控制器:
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
UIViewController 没有名为 txtInTab2 的属性。 txtInTab2 是您添加到您自己的 UIViewController 子类(您称为 TabBar2View)的属性。
代码基本上是正确的,但是您需要将 tab2 定义为它实际上是什么类型的视图控制器。从下面的评论来看,它实际上是一个导航控制器,因此您需要执行以下操作:
然后要实际显示它,您需要通过选项卡栏控制器执行此操作,如下所示:
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:
Then to actually show it, you need to do this via the tab bar controller, like this:
您需要使用代表。我认为 这个会给你一个关于如何开始的很好的帮助
You need to use delegates. I think this will give you a good help about how to get started