第一个视图中的 UISwitch 和第二个视图中的标签

发布于 2024-12-03 20:13:11 字数 314 浏览 2 评论 0原文

因此,我想在第一个视图中放置一个标签,并在第二个视图中放置一个 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 技术交流群。

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

发布评论

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

评论(2

计㈡愣 2024-12-10 20:13:11

确实你也不是很清楚。您可能想要尝试的第一件事是描述您所做的事情:

  • 您的两个视图是如何实例化的?

假设您的两个视图是从两个不同的 nib 文件实例化的。

  • 您想要访问您的标签和开关的对象是什么?

我们假设它是一个视图控制器。对于单个视图控制器来说,从两个不同的 nib 文件控制两个视图有点不寻常,但毕竟,为什么不呢?

在任何情况下,您都可以将两个 nib 文件的所有者类设置为视图控制器的类。然后在 Interface Builder 中,从第一个视图中,您可以将标签绑定到文件所有者的 UILabel 出口。在 Interface Builder 中,从第二个视图中,您可以将 UISwitch 绑定到文件所有者的第二个出口(类型为 UISwitch)。

但也许您的 onRoff 方法实际上是您的两个视图类之一的方法?同样的想法也适用:您可以将第二个 nib 文件中的文件所有者设置为第一个视图的视图类,然后将开关绑定到文件所有者的 UISwitch 出口。

但听起来你的设计可能值得研究......

编辑:在你的评论之后,这里还有更多......

问题是你的两个视图控制器各自控制不同的页面并且没有理由互相了解。所以你需要一个中间人对象。那可能是另一个控制器。让我们使用应用程序委托。然后,在 SwitchViewController 的 IBAction 方法中,您可以执行以下操作:

- (IBAction) switchChangedValue:(UISwitch *) sender {
  NSString *newLabelText = sender.isOn ? @"On" : @"Off";
  self.labelViewController.label.text = newLabelText;
}

现在每个人如何相互了解?首先,每个视图控制器都会通知中间人。这是 SwitchViewController 的内容:

- (void) viewDidLoad
{
  [super viewDidLoad];
  MyAppDelegate *appDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
  appDelegate.switchViewController = self;
}

其次,应用程序委托需要协调一切:

@interface MyAppDelegate : …
@property (nonatomic, retain) SwitchViewController *switchViewController;
@property (nonatomic, retain) LabelViewController *labelViewController;
@end

@implementation MyAppDelegate

@synthesize switchViewController = _switchViewController;
@synthesize labelViewController = _labelViewController;

- (void) setSwitchViewController:(SwitchViewController *) newSwitchController {
  if (newSwitchController != _switchViewController) {
    [_switchViewController release];
    _switchViewController = [newSwitchController retain];
    _switchViewController.labelViewController = _labelViewController;
    if (_labelViewController)
      _labelViewController.label.text = _switchViewController.switch.isOn ? @"On" : @"Off";
  }
}

- (void) setLabelViewController:(LabelViewController *) newLabelController {
  if (newLabelController != _labelViewController) {
    [_labelViewController release];
    _labelViewController = [newLabelController retain];
    _labelViewController.switchViewController = _switchViewController;
    if (_switchViewController)
      _labelViewController.label.text = _switchViewController.switch.isOn ? @"On" : @"Off";
  }
}

我遗漏了一些细节,但我希望大局是清晰的。

Indeed you are not very clear. The first thing you might want to try is describe what you did:

  • how are your two views instantiated?

Let's assume your two views are instantiated from two different nib files.

  • what is the object you want to have access to your label and switch?

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 the UISwitch to the file owner's second outlet, of type UISwitch.

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's UISwitch 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:

- (IBAction) switchChangedValue:(UISwitch *) sender {
  NSString *newLabelText = sender.isOn ? @"On" : @"Off";
  self.labelViewController.label.text = newLabelText;
}

Now how will everybody know about each other? First each view controller will inform the middle man. Here is it for the SwitchViewController:

- (void) viewDidLoad
{
  [super viewDidLoad];
  MyAppDelegate *appDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
  appDelegate.switchViewController = self;
}

Second, the app delegate will need to coordinate everything:

@interface MyAppDelegate : …
@property (nonatomic, retain) SwitchViewController *switchViewController;
@property (nonatomic, retain) LabelViewController *labelViewController;
@end

@implementation MyAppDelegate

@synthesize switchViewController = _switchViewController;
@synthesize labelViewController = _labelViewController;

- (void) setSwitchViewController:(SwitchViewController *) newSwitchController {
  if (newSwitchController != _switchViewController) {
    [_switchViewController release];
    _switchViewController = [newSwitchController retain];
    _switchViewController.labelViewController = _labelViewController;
    if (_labelViewController)
      _labelViewController.label.text = _switchViewController.switch.isOn ? @"On" : @"Off";
  }
}

- (void) setLabelViewController:(LabelViewController *) newLabelController {
  if (newLabelController != _labelViewController) {
    [_labelViewController release];
    _labelViewController = [newLabelController retain];
    _labelViewController.switchViewController = _switchViewController;
    if (_switchViewController)
      _labelViewController.label.text = _switchViewController.switch.isOn ? @"On" : @"Off";
  }
}

I left out a number of details, but I hope the big picture is clear.

云归处 2024-12-10 20:13:11

所以你已经在第二个视图中声明了你的 UISwitch 并在第一个视图中声明了你的标签。你所要做的就是使用 NSUserDefaults 来实现你想要的。在第二个视图本身中有以下方法。不要将其带到第一视图。

- (void)onRoff {

   if (mySwitch1.on) {
    [[NSUserDefaults standarduserdefaults]setObject:@"off" forKey:@"state"];
    [[NSUserDefaults standarduserdefaults]synchronize];
   }
   else {
     [[NSUserDefaults standarduserdefaults]setObject:@"on" forKey:@"state"];
     [[NSUserDefaults standarduserdefaults]synchronize];
   }

}

现在,在第一个视图的 viewWillAppear 方法中,只需检查 NSUserDefaults 的值。

-(void)chkState{
NSString *tempStr=[[NSUserDefaults standarduserdefaults]objectForKey:@"state"];
   if([tempStr isEqualTo:@"on"]) {
      test.hidden=YES;
   }
   else {
      test.hidden=NO;
   }
}

在第一个视图的 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.

- (void)onRoff {

   if (mySwitch1.on) {
    [[NSUserDefaults standarduserdefaults]setObject:@"off" forKey:@"state"];
    [[NSUserDefaults standarduserdefaults]synchronize];
   }
   else {
     [[NSUserDefaults standarduserdefaults]setObject:@"on" forKey:@"state"];
     [[NSUserDefaults standarduserdefaults]synchronize];
   }

}

Now in the viewWillAppear method of the first view just chk the value of the NSUserDefaults..

-(void)chkState{
NSString *tempStr=[[NSUserDefaults standarduserdefaults]objectForKey:@"state"];
   if([tempStr isEqualTo:@"on"]) {
      test.hidden=YES;
   }
   else {
      test.hidden=NO;
   }
}

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

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