UIApplication sharedApplication - keyWindow是否为零? [英] UIApplication sharedApplication - keyWindow is nil?

查看:103
本文介绍了UIApplication sharedApplication - keyWindow是否为零?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将CGPoint从我的UIView转换为UIWindow坐标,并且已经意识到UIApplication keyWindow总是为零;这是为什么?

I want to convert a CGPoint from my UIView to UIWindow coordinates and have realized that UIApplication keyWindow is always nil; why is this?

我已经尝试过UIView的 convertPoint:toView:方法。

I have tried the convertPoint:toView: method from UIView.

请参阅我在Xcode模板中查看控制器中的示例代码(查看应用程序):

Please see this sample code I tried in the view controller in a template of Xcode (View application):

- (void)viewDidLoad {
    [super viewDidLoad];
    UIView *test =  [[UIView alloc] initWithFrame:CGRectMake(40,40,250,250)];
    [test setBackgroundColor:[UIColor redColor]];
    [self.view addSubview:test];

    CGPoint p = CGPointMake(100, 100);
    CGPoint np;

    np = [test convertPoint:p toView:[[UIApplication sharedApplication] keyWindow]];
    NSLog(@"p:%@ np:%@", NSStringFromCGPoint(p), NSStringFromCGPoint(np));

    AppDelegate *appDel =  (AppDelegate *)[UIApplication sharedApplication].delegate;

    np = [test convertPoint:p toView:[appDel window]];
    NSLog(@"p:%@ np:%@", NSStringFromCGPoint(p), NSStringFromCGPoint(np));

    np = [test convertPoint:p toView:nil];
    NSLog(@"p:%@ np:%@", NSStringFromCGPoint(p), NSStringFromCGPoint(np));

    [test release];

    if(![[UIApplication sharedApplication] keyWindow])
        NSLog(@"window was nil");
}

我得到:

p:{100, 100} np:{100, 100}
p:{100, 100} np:{140, 160}
p:{100, 100} np:{100, 100}
window was nil

我可以转换它但只有当我通过app委托访问窗口时。而不是UIApplication。根据文档,keyWindow应该在这里工作,但是没有。
为什么会这样?

I can convert it but only when I access the window through the app delegate. And not UIApplication. According to the documentation, keyWindow should work here, but is nil. Why is this?

推荐答案

此代码在 [window makeKeyAndVisible]之前执行; 这是在app代理中。
所以,难怪为什么 keyWindow 还是 nil

This code was executed before [window makeKeyAndVisible]; which is inside the app delegate. So, no wonder why keyWindow was nil yet.

这篇关于UIApplication sharedApplication - keyWindow是否为零?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆