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 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
  • ¥50 NT4.0系统 STOP:0X0000007B
  • ¥15 想问一下stata17中这段代码哪里有问题呀