Objective c 在Xcode中将对象从一个xib声明到另一个?

Objective c 在Xcode中将对象从一个xib声明到另一个?,objective-c,xcode,viewcontroller,declare,Objective C,Xcode,Viewcontroller,Declare,我有一个项目,它有一个与主ViewController分离的xib开关。我正在尝试使其在您将UISwitch切换到OFF时,ViewController中的一个按钮被隐藏。我试图在xib的.m文件中声明AppDelegate,但仍然没有成功。我的代码是: - (void)viewDidAppear:(BOOL)animated { AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] deleg

我有一个项目,它有一个与主ViewController分离的xib开关。我正在尝试使其在您将UISwitch切换到OFF时,ViewController中的一个按钮被隐藏。我试图在xib的.m文件中声明AppDelegate,但仍然没有成功。我的代码是:

- (void)viewDidAppear:(BOOL)animated {

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

我做错了什么?我正在努力

if (switch.on) {
     myButton.hidden = YES;
   } else {
     myButton.hidden = NO;
}
我在第二个xib(没有主ViewController的xib)的.m文件中也这样做了

但还是没什么!所以基本上,我只是想从一个xib中声明一个按钮。就这样。请帮忙,谢谢

首先,请参见和

一旦您理解了这一点,就可以像这样设置代码--

FirstViewController.h:

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

- (IBAction)switchToggled;

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

@end
SecondViewController.h:

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

- (IBAction)switchToggled;

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

@end
确保以某种方式设置了FirstViewController,再次请参见。然后在
开关切换中
可以执行以下操作:

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

需要记住的重要一点是,视图控制器之间并没有神奇的相互了解。即使您使用AppDelegate做了一些您正在尝试的事情
SecondViewController
需要对
FirstViewController
的引用

第二个xib在同一个故事板上吗?如中所示,两个视图控制器都出现在同一个故事板屏幕上,或者它是一个单独的nib文件,不与视图控制器一起出现在故事板上?@LuisOscar我没有使用故事板。在我的项目文件夹中,有AppDelegate.h、AppDelegate.m、ViewController.xib、ViewnController.h和ViewController.m。在我的第二个xib文件夹中,(我将其命名为settings)settings.h、settings.m和settings.xib我应该怎么做。