第一个视图中的 UISwitch 和第二个视图中的标签
因此,我想在第一个视图中放置一个标签,并在第二个视图中放置一个 UISwitch。 但问题是我无法将所有内容链接在一起..:/
在我的第一个视图中我有这个
- (void)onRoff {
if (mySwitch1.on) {
test.hidden = YES;
}
else (test.hidden = NO);
}
,但在这里我有一个 mySwitch1 错误,因为它是在我的第二个视图中声明的。 我不知道是否清楚,我想在不同视图中链接标签和开关..
谢谢!
So, I want to place a label in my fist view and place in a second one a UISwitch.
But the problem is I can't link together everything.. :/
in my first view i have that
- (void)onRoff {
if (mySwitch1.on) {
test.hidden = YES;
}
else (test.hidden = NO);
}
but here I have an error with mySwitch1 because it's declared in my secondView..
I don't know if it's clear, I want to link a label and a switch in different view..
Thanks !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
确实你也不是很清楚。您可能想要尝试的第一件事是描述您所做的事情:
假设您的两个视图是从两个不同的 nib 文件实例化的。
我们假设它是一个视图控制器。对于单个视图控制器来说,从两个不同的 nib 文件控制两个视图有点不寻常,但毕竟,为什么不呢?
在任何情况下,您都可以将两个 nib 文件的所有者类设置为视图控制器的类。然后在 Interface Builder 中,从第一个视图中,您可以将标签绑定到文件所有者的
UILabel
出口。在 Interface Builder 中,从第二个视图中,您可以将UISwitch
绑定到文件所有者的第二个出口(类型为UISwitch
)。但也许您的 onRoff 方法实际上是您的两个视图类之一的方法?同样的想法也适用:您可以将第二个 nib 文件中的文件所有者设置为第一个视图的视图类,然后将开关绑定到文件所有者的
UISwitch
出口。但听起来你的设计可能值得研究......
编辑:在你的评论之后,这里还有更多......
问题是你的两个视图控制器各自控制不同的页面并且没有理由互相了解。所以你需要一个中间人对象。那可能是另一个控制器。让我们使用应用程序委托。然后,在 SwitchViewController 的
IBAction
方法中,您可以执行以下操作:现在每个人如何相互了解?首先,每个视图控制器都会通知中间人。这是 SwitchViewController 的内容:
其次,应用程序委托需要协调一切:
我遗漏了一些细节,但我希望大局是清晰的。
Indeed you are not very clear. The first thing you might want to try is describe what you did:
Let's assume your two views are instantiated from two different nib files.
Let's assume it's a view controller. It's a bit unusual for a single view controller to control two views from two different nib files, but after all, why not?
In any case, you can set the owner class for your two nib files to be the class of your view controller. Then in Interface Builder, from the first view, you can bind the label to the file owner's
UILabel
outlet. And in Interface Builder, from the second view, you can bind theUISwitch
to the file owner's second outlet, of typeUISwitch
.But perhaps the
onRoff
methods of yours is actually a method of one of your two view class? The same idea apply: you can set the file owner in the second nib file to be the view class of the first view, and then bind the switch to the file owner'sUISwitch
outlet.But it sounds like your design might be worth working on...
Edit: after your comment, here is a bit more...
The problem is that your two view controllers each control a different page and have no reason to know about each other. So you need a middle man object. That could be another controller. Let's use the Application delegate. Then, in the
IBAction
method of your SwitchViewController, you can do something like:Now how will everybody know about each other? First each view controller will inform the middle man. Here is it for the SwitchViewController:
Second, the app delegate will need to coordinate everything:
I left out a number of details, but I hope the big picture is clear.
所以你已经在第二个视图中声明了你的 UISwitch 并在第一个视图中声明了你的标签。你所要做的就是使用 NSUserDefaults 来实现你想要的。在第二个视图本身中有以下方法。不要将其带到第一视图。
现在,在第一个视图的 viewWillAppear 方法中,只需检查 NSUserDefaults 的值。
在第一个视图的 viewWillAppear 中调用此方法,如下所示....
[自我检查状态];
希望这会有所帮助......如果您也想保存开关的状态,那么只需在第二个视图的 viewWilAppear 方法中再次检查 userdefaults 值并基于
So you have declared ur UISwitch in the second view and ur label in the first view. All u have to do is just use NSUserDefaults to achieve wat u want. Have the following method in the second view itself. Dont bring it to the first view.
Now in the viewWillAppear method of the first view just chk the value of the NSUserDefaults..
Call this method in the viewWillAppear of the firstview like this....
[self chkState];
Hope this helps....If u want save the state of the switch too then just chk the userdefaults value again in the viewWilAppear method of the 2nd view and based