我想将CGPoint从我的UIView转换为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];
之前执行。
因此,难怪keyWindow
为什么现在是nil
。
关于iphone - UIApplication sharedApplication-keyWindow为零?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/3359501/