在APPdelegate代理方法中 设置window属性编译没问题 运行报错?

在APPdelegate代理类中自己写了如下代码?

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    CGRect firstFrame = CGRectMake(160, 240, 100, 150);
    BNRHypnosisView *firstView = [[BNRHypnosisView alloc] initWithFrame:firstFrame];
    firstView.backgroundColor = [UIColor redColor];
    [self.window addSubview:firstView];

    self.window.backgroundColor = [UIColor blueColor];
    [self.window makeKeyAndVisible];
    return YES;
}
//程序完成启动后,会给AppDelegate对象发送这条消息.

其中BNRHypnosisView 是一个 继承于UIView的类.

编译时没报错.
程序运行崩溃,错误信息显示:

2017-04-17 14:24:47.878 Views and View Hierarchy[31077:1917671] *** Assertion failure in -[UIApplication runWithMainScene:transitionContext:completion:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-3600.7.47/UIApplication.m:3677
2017-04-17 14:24:47.880 Views and View Hierarchy[31077:1917671] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Application windows are expected to have a root view controller at the end of application launch'
*** First throw call stack:
(
0 CoreFoundation 0x000000010852db0b __exceptionPreprocess + 171
1 libobjc.A.dylib 0x00000001060ca141 objc_exception_throw + 48
2 CoreFoundation 0x0000000108531cf2 +[NSException raise:format:arguments:] + 98
3 Foundation 0x0000000105c643b6 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 193
4 UIKit 0x000000010659abe6 -[UIApplication _runWithMainScene:transitionContext:completion:] + 3343
5 UIKit 0x0000000106597793 -[UIApplication workspaceDidEndTransaction:] + 182
6 FrontBoardServices 0x0000000109c3d5f6 __FBSSERIALQUEUE_IS_CALLING_OUT_TO_A_BLOCK
_ + 24
7 FrontBoardServices 0x0000000109c3d46d -[FBSSerialQueue performNext] + 186
8 FrontBoardServices 0x0000000109c3d7f6 -[FBSSerialQueue _performNextFromRunLoopSource] + 45
9 CoreFoundation 0x00000001084d3c01 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION
_ + 17
10 CoreFoundation 0x00000001084b90cf __CFRunLoopDoSources0 + 527
11 CoreFoundation 0x00000001084b85ff __CFRunLoopRun + 911
12 CoreFoundation 0x00000001084b8016 CFRunLoopRunSpecific + 406
13 UIKit 0x000000010659602f -[UIApplication _run] + 468
14 UIKit 0x000000010659c0d4 UIApplicationMain + 159
15 Views and View Hierarchy 0x0000000105afc9ff main + 111
16 libdyld.dylib 0x00000001094cd65d start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)

不懂了,这是设置问题还是Xcode版本问题?代码我是按照书上照抄的呀.

self.window.rootViewController=firstView;
不是[self.window addSubview:firstView];

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Application windows are expected to have a root view controller at the end of application launch'
这个错误提示就是告诉你,app启动后必须设置根控制器。
self.window.rootViewController=xxx;//这个必须要设置

[self.window addSubview:firstView]; 错误
标准写法是
BNRHypnosisView *firstView = [[BNRHypnosisView alloc] initWithFrame:firstFrame];
firstView.backgroundColor = [UIColor redColor];
self.window.rootViewController= firstView;

楼上真是瞎扯。。。。都说了,根控制器,结果拿一个View去当Controller了。。。笑死人了~

Application windows are expected to have a root view controller at the end of application launch',这句话已经说得很清楚了啊,没设置根控制器就要添加view,当然会报错

我也遇到过这个问题,在self.window做任何操作之前,是需要设置它的rootViewController的,看你直接在上面加了一个view。可以有两种方式解决你的问题:
1、把你的那个view放到一个controller里面,在你这里初始化你的controller,然后设置self.window.rootViewController=你的controller
2、self.window.rootViewController = [[UIViewController alloc] init];然后再add你的这个view在self.window上。代码如下:
self.window.rootViewController = [[UIViewController alloc] init];
UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
view.backgroundColor = RGB(130, 43, 123);
[self.window addSubview:view];