开发者

UISwitch in a first view and a label in a second

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 i开发者_Go百科t'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 !


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜