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 ELGamal和paillier计算效率谁快?
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 ubuntu系统下挂载磁盘上执行./提示权限不够
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码