【问题标题】:Declaring objects from one xib to another in Xcode?在 Xcode 中将对象从一个 xib 声明到另一个?
【发布时间】:2012-05-11 00:09:37
【问题描述】:

我有一个项目,它有一个与主 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的那个)的.m文件中做了这个

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

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

【问题讨论】:

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

标签: objective-c xcode viewcontroller declare


【解决方案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;
}

要记住的重要一点是,您的视图控制器不会神奇地相互了解。即使您像尝试那样使用 AppDelegate 做某事。 SecondViewController 需要引用 FirstViewController

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-07
    • 1970-01-01
    相关资源
    最近更新 更多