问题 如果想要使用主故事板文件,则app委托必须实现window属性


我是以编程方式实现列表视图控制器。 当我尝试运行该项目时,我收到错误:

2012-11-07 22:46:34.719 myTableViewControl[12021:c07] The app delegate must implement the         window property if it wants to use a main storyboard file.
2012-11-07 22:46:34.722 myTableViewControl[12021:c07] -[AppDelegate setWindow:]:     unrecognized selector sent to instance 0x7674e70
2012-11-07 22:46:34.723 myTableViewControl[12021:c07] *** Terminating app due to uncaught     exception 'NSInvalidArgumentException', reason: '-[AppDelegate setWindow:]: unrecognized     selector sent to instance 0x7674e70'
*** First throw call stack:
(0x1c8e012 0x10cbe7e 0x1d194bd 0x10df7ea 0x1c7dcf9 0x1c7d94e 0x1d60 0x107b7 0x10da7     0x11fab 0x23315 0x2424b 0x15cf8 0x1be9df9 0x1be9ad0 0x1c03bf5 0x1c03962 0x1c34bb6 0x1c33f44     0x1c33e1b 0x117da 0x1365c 0x1bd2 0x1b05)
libc++abi.dylib: terminate called throwing an exception
(lldb) 

当我运行代码时,它挂在main.m上并显示出来

“thread1:信号SIGABRT”

@autoreleasepool {return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));

我的代码如下: AppDelegate.h

//
//  AppDelegate.h
//  myTableViewControl
//
//  Created by Max on 12-11-5.
//  Copyright (c) 2012年 Max. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UINavigationController *navigationController;


@end

AppDelegate.m

//
//  AppDelegate.m
//  myTableViewControl
//
//  Created by Max on 12-11-5.
//  Copyright (c) 2012年 Max. All rights reserved.
//

    #import "AppDelegate.h"
    #import "firstViewController.h"


@implementation AppDelegate
@synthesize navigationController;


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:    (NSDictionary *)launchOptions
{
    // create the base window
    UIWindow *window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    window.backgroundColor = [UIColor greenColor];
    self.window = window;

    [window release];

    // this is the home page from the user's perspective

    FirstViewController *fvc = [[FirstViewController alloc] init];

    UINavigationController *nc = [[UINavigationController alloc]initWithRootViewController:fvc];
    self.navigationController = nc;

    [fvc release];
    [nc release];

    // show them
    [self.window addSubview: nc.view];
    [self.window makeKeyAndVisible];

    return YES;
}

@end

FirstViewController是列表视图控制器。


7258
2017-11-07 15:04


起源



答案:


您正在将窗口创建为局部变量,然后尝试使用它来访问它,就像它是属性一样 self.window。把它变成一个财产。


16
2017-11-07 15:07



菲利普是对的。您在头文件中缺少此行: @property (strong, nonatomic) UIWindow *window; - Cashew
thnx所以......太多了。我是ios上的新人,非常感谢你的帮助 - max
此外,当你已经拥有它 @property 它仍然会引发运行时错误,看看你是否也有 @synthesize 为了它。它奇迹般地奇迹般地 没有它的工作 对于XCode 4.5和iPhone 5.我没有尝试过任何其他组合,因为我事先发现了这个错误。 - Pavel Zdenek
@PavelZdenek是的,这是由于新的Objective-C 2.0功能,自动合成属性。 - Matias Forbord


答案:


您正在将窗口创建为局部变量,然后尝试使用它来访问它,就像它是属性一样 self.window。把它变成一个财产。


16
2017-11-07 15:07



菲利普是对的。您在头文件中缺少此行: @property (strong, nonatomic) UIWindow *window; - Cashew
thnx所以......太多了。我是ios上的新人,非常感谢你的帮助 - max
此外,当你已经拥有它 @property 它仍然会引发运行时错误,看看你是否也有 @synthesize 为了它。它奇迹般地奇迹般地 没有它的工作 对于XCode 4.5和iPhone 5.我没有尝试过任何其他组合,因为我事先发现了这个错误。 - Pavel Zdenek
@PavelZdenek是的,这是由于新的Objective-C 2.0功能,自动合成属性。 - Matias Forbord