gpt4 book ai didi

iphone - UIApplication sharedApplication - keyWindow 为零?

转载 作者:太空狗 更新时间:2023-10-30 03:09:08 28 4
gpt4 key购买 nike

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

我尝试了 UIView 中的 convertPoint:toView: 方法。

请查看我在 Xcode(View 应用程序)模板中的 View Controller 中尝试的示例代码:

- (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

我可以转换它,但只有当我通过应用程序委托(delegate)访问窗口时才可以。而不是 UIApplication。根据文档, keyWindow 应该在这里工作,但为零。这是为什么?

最佳答案

此代码在应用委托(delegate)内的 [window makeKeyAndVisible]; 之前执行。所以,难怪 keyWindownil 了。

关于iphone - UIApplication sharedApplication - keyWindow 为零?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3359501/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com