Objective c 以编程方式加载Nib

Objective c 以编程方式加载Nib,objective-c,ios,xcode,ios5,nib,Objective C,Ios,Xcode,Ios5,Nib,我在Xcode 4.3.2中工作 我试图以编程方式加载Nib,而不是使用故事板。故事板看起来简单得多,但不幸的是它们与iOS4不兼容&我正在构建一个需要向后兼容的应用程序 这是我的问题:我试图加载主视图-一个带有绿色背景的简单视图。目前没有默认视图 我的档案是: AppDelegate.h/.m GreenViewController.h/.m SwitchViewController.h/.m 我想在应用程序开始时加载绿色屏幕,因此在AppDelegate.m中我有: - (BOOL)appl

我在Xcode 4.3.2中工作

我试图以编程方式加载Nib,而不是使用故事板。故事板看起来简单得多,但不幸的是它们与iOS4不兼容&我正在构建一个需要向后兼容的应用程序

这是我的问题:我试图加载主视图-一个带有绿色背景的简单视图。目前没有默认视图

我的档案是:

AppDelegate.h/.m GreenViewController.h/.m SwitchViewController.h/.m

我想在应用程序开始时加载绿色屏幕,因此在AppDelegate.m中我有:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.switchViewController = [[SwitchViewController alloc] initWithNibName:@"GreenView" bundle:nil];

    //UIView *switchView = self.switchViewController.view;


    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}
- (void)viewDidLoad
{
    self.greenViewController = [[GreenViewController alloc] initWithNibName:@"GreenView" bundle:nil];
    [self.view insertSubview:self.greenViewController.view atIndex:0];
    [super viewDidLoad];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
在SwitchViewController.m中,我有:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.switchViewController = [[SwitchViewController alloc] initWithNibName:@"GreenView" bundle:nil];

    //UIView *switchView = self.switchViewController.view;


    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}
- (void)viewDidLoad
{
    self.greenViewController = [[GreenViewController alloc] initWithNibName:@"GreenView" bundle:nil];
    [self.view insertSubview:self.greenViewController.view atIndex:0];
    [super viewDidLoad];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
用默认代码填充简单的initWithNibName

在GreenViewController.m中,我有:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.switchViewController = [[SwitchViewController alloc] initWithNibName:@"GreenView" bundle:nil];

    //UIView *switchView = self.switchViewController.view;


    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}
- (void)viewDidLoad
{
    self.greenViewController = [[GreenViewController alloc] initWithNibName:@"GreenView" bundle:nil];
    [self.view insertSubview:self.greenViewController.view atIndex:0];
    [super viewDidLoad];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
以下是所有内容的主要功能。当我运行应用程序时,我立即得到错误:

2012-10-09 12:34:35.586 MyViewSwitcher[5210:f803] -[AppDelegate setSwitchViewController:]: unrecognized selector sent to instance 0x6c6c310
2012-10-09 12:34:35.587 MyViewSwitcher[5210:f803] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[AppDelegate setSwitchViewController:]: unrecognized selector sent to instance 0x6c6c310'
有人能帮我理解我错误的原因吗?我确保GreenView.xib的“自定义类”是Identity Inspector中的“GreenViewController”。此外,我还确保“视图”的引用出口是文件的所有者。我想在我的应用程序启动时加载GreenView nib。最后,我将通过添加一个按钮来切换视图,但到目前为止,我甚至无法加载主视图。我不确定我在说什么

提前感谢您的帮助

 '-[AppDelegate setSwitchViewController:]: unrecognized selector sent to instance 0x6c6c310'
您正在向AppDelegate发送消息(调用该方法)
setSwitchViewController:
。这就是问题所在


您正在向AppDelegate发送消息(调用该方法)
setSwitchViewController:
。这就是问题所在。

问题不在于加载nib。问题出在这一行:

self.switchViewController = [[SwitchViewController alloc] initWithNibName:@"GreenView" bundle:nil];
这里使用的是“点语法”,如中所述。当使用该语法设置属性时,编译器会将其转换为常规的Objective-C消息,如下所示:

[self setSwitchViewController:[[SwitchViewController alloc] initWithNibName:@"GreenView" bundle:nil]];
在运行时,系统会告诉您您的
AppDelegate
对象不理解
setSwitchViewController:
方法。您需要使用
@synthesis
指令来告诉编译器为您实现
setSwitchViewController:
方法:

@synthesize switchViewController = _switchViewController;

问题不在于加载nib。问题在这一行:

self.switchViewController = [[SwitchViewController alloc] initWithNibName:@"GreenView" bundle:nil];
这里使用的是“点语法”,如中所述。当使用该语法设置属性时,编译器会将其转换为常规的Objective-C消息,如下所示:

[self setSwitchViewController:[[SwitchViewController alloc] initWithNibName:@"GreenView" bundle:nil]];
在运行时,系统会告诉您您的
AppDelegate
对象不理解
setSwitchViewController:
方法。您需要使用
@synthesis
指令来告诉编译器为您实现
setSwitchViewController:
方法:

@synthesize switchViewController = _switchViewController;
  • 在使用
    makeKeyAndVisible之前,请确保您的
    UIWindow
    具有
    rootViewController

    [self.windowsetRootViewController:self.switchViewController]; [self.window makeKeyAndVisible]

  • 确保按照上面的说明合成属性

  • 在子类中实现方法时,如
    SwitchViewController.m
    ,何时调用
    super
    方法很重要。除非您有特殊意图,否则几乎总是在本地
    self
    实现之前调用
    super
    实现。因此,我建议您更改
    -(void)在
    开关viewcontroller.m中加载viewDidLoad
    ,如下所示

    • (无效)viewDidLoad{ [超级视图下载]

      self.greenViewController=[[greenViewController alloc]initWithNibName:@“GreenView”bundle:nil]

      [self.view insertSubview:self.greenViewController.view索引:0]; }

  • 在使用
    makeKeyAndVisible之前,请确保您的
    UIWindow
    具有
    rootViewController

    [self.windowsetRootViewController:self.switchViewController]; [self.window makeKeyAndVisible]

  • 确保按照上面的说明合成属性

  • 在子类中实现方法时,如
    SwitchViewController.m
    ,何时调用
    super
    方法很重要。除非您有特殊意图,否则几乎总是在本地
    self
    实现之前调用
    super
    实现。因此,我建议您更改
    -(void)在
    开关viewcontroller.m中加载viewDidLoad
    ,如下所示

    • (无效)viewDidLoad{ [超级视图下载]

      self.greenViewController=[[greenViewController alloc]initWithNibName:@“GreenView”bundle:nil]

      [self.view insertSubview:self.greenViewController.view索引:0]; }


  • 你是否正确地合成了你的
    switchViewController
    属性?阿迪尔,这肯定是我的问题。我添加了“@synthesis-switchViewController;”&现在我的错误是:2012-10-09 13:48:27.970 MyViewSwitcher[5345:f803]应用程序窗口应该在应用程序启动结束时有一个根视图控制器。你能帮我一把吗?我想我走对了。你是否正确地合成了
    switchViewController
    属性?Adil,这肯定是我的问题。我添加了“@synthesis switchViewController;”&现在我的错误是:2012-10-09 13:48:27.970 MyViewSwitcher[5345:f803]应用程序窗口应该在应用程序启动结束时有一个根视图控制器。你能帮我一下吗?我想我走对了方向。我如何调用方法setSwitchViewController?我还没有定义它。我是在某处隐式调用它吗?我如何调用方法setSwitchViewController?我还没有定义奈德,我是在暗示什么吗?整洁的身材也有帮助。谢谢你,罗布。整洁的身材也有帮助。谢谢你,罗布。