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

时间:2012-05-11 00:09:38

标签: objective-c xcode viewcontroller declare

我的项目有一个xib与主ViewController分开的切换。我试图这样做,以便当您将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的.m文件中做了这个(没有主ViewController的那个)

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

但仍然没有!所以基本上,我只是试图在另一个xib中声明一个按钮。而已。请帮助谢谢!

1 个答案:

答案 0 :(得分:1)

首先,请参阅this about passing data between view controllersthis about UISwitch

一旦理解了这一点,请设置类似的代码 -

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

请确保以某种方式设置theFirstViewController - 再次查看passing data between view controllers。然后在switchToggled中你可以这样做:

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

要记住的重要一点是,您的视图控制器不会神奇地了解彼此。即使您像AppODlegate一样尝试了一些事情。 SecondViewController需要引用FirstViewController

相关问题