0

I have a project that has a switch from an xib separate from the main ViewController. I am trying to make it so that when you switch the UISwitch to OFF a button in the ViewController is hidden. I have tried to declare the AppDelegate in the xib's .m file but still no luck. My code is:

- (void)viewDidAppear:(BOOL)animated {

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];

}

What am I doing wrong? I am trying to do

if (switch.on) {
     myButton.hidden = YES;
   } else {
     myButton.hidden = NO;
}

I have also done this in the .m file of the second xib (the one that does not have the main ViewController)

#import "AppDelegate.h"
#import "ViewController.h"

But still NOTHING! So basically, I'm just trying to declare a button in one xib from another. THAT'S IT. PLEASE HELP Thanks!

  • is the second xib on the same story board? as in, both view controllers appear on the same storyboard screen, or is it a separate nib file that doesnt appear on the storyboard along with the viewcontroller? – Pochi May 11 '12 at 00:30
  • @LuisOscar I am not using storyboard. In my Project folder, I have AppDelegate.h, AppDelegate.m, ViewController.xib, ViewnController.h, and ViewController.m. In my 2nd Folder for my 2nd xib, (I named it settings) settings.h, settings.m, and settings.xib what should I do. – Big Box Developer May 11 '12 at 00:42

1 Answers1

1

First, see this about passing data between view controllers and this about UISwitch.

Once you understand that, set up your code something like this--

FirstViewController.h:

@interface FirstViewController : UIViewController
{
  ...
}
@property IBOutlet UISwitch *theSwitch;

- (IBAction)switchToggled;

@end

SecondViewController.h:

@interface SecondViewController : UIViewController
{
  ...
}
@property IBOutlet UIButton *button;
@property UIViewController *theFirstViewController;

@end

Be sure that theFirstViewController gets set somehow -- again, see passing data between view controllers. Then in switchToggled you can do this:

- (IBAction)switchToggled
{
  theFirstViewController.button.hidden = YES;
}

The important thing to remember is that your view controllers don't magically know about each other. Even if you do something with the AppDelegate like you were trying. SecondViewController needs a reference to FirstViewController.

Community
  • 1
  • 1
jlstrecker
  • 4,953
  • 3
  • 46
  • 60