dominghao 2017-04-17 06:30 采纳率: 33.3%
浏览 2813

在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版本问题?代码我是按照书上照抄的呀.

  • 写回答

6条回答 默认 最新

  • buyu03 2017-04-18 03:45
    关注

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

    评论

报告相同问题?

悬赏问题

  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误