UIApplication共享应用程序-keyWindow为零?

发布于 2024-09-11 12:40:37 字数 1317 浏览 2 评论 0原文

我想将 CGPoint 从 UIView 转换为 UIWindow 坐标,并意识到 UIApplication keyWindow 始终为零;这是为什么?

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

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

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

我可以转换它,但只有当我通过应用程序委托访问窗口时。而不是 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?

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

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");
}

and I get:

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

爱已欠费 2024-09-18 12:40:37

此代码是在应用程序委托内部的 [window makeKeyAndVisible]; 之前执行的。
所以,难怪为什么 keyWindow 仍然是 nil

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

本宫微胖 2024-09-18 12:40:37

最简单的方法是从应用程序委托获取窗口:

UIWindow *keyWindow = [[[UIApplication sharedApplication] delegate] window];
// Do something with the window now

Easiest way is to get the window from the app delegate instead:

UIWindow *keyWindow = [[[UIApplication sharedApplication] delegate] window];
// Do something with the window now
壹場煙雨 2024-09-18 12:40:37

我注意到启动引导访问后,[UIApplication sharedApplication] 上的 keyWindow 属性似乎为零。

仅在 iOS7 上,当我在“设置”中启用“引导访问模式”后第一次启动“引导访问模式”时,才会发生这种情况。一般>引导式访问,因此实际显示起始 GAM 视图而不是绕过。

由于这个 Apple API 似乎有问题,我解决了使用以下代码来检索我正在寻找的窗口的问题。

NSArray *windows = [[UIApplication sharedApplication] windows];
if ([windows count]) {
    return windows[0];
}
return nil;

也许

[[UIApplication sharedApplication] keyWindow];

您也可以尝试使用

[[[UIApplication sharedApplication] delegate] window];

iWasRobbed 指出的那样,但它对我来说不起作用,因为 rootViewController通过这种方式无法访问 属性。

I noticed that after having started the Guided Access, the keyWindow property on [UIApplication sharedApplication] appears to be nil.

It happened to me only on iOS7 when starting the Guided Access Mode for the first time after having enabled it in Settings > General > Guided Access, so the starting GAM view is actually displayed and not by-passed.

Since this Apple API seems buggy, I solved using the following code to retrieve the window I'm looking for.

NSArray *windows = [[UIApplication sharedApplication] windows];
if ([windows count]) {
    return windows[0];
}
return nil;

Instead of

[[UIApplication sharedApplication] keyWindow];

maybe you could also try using

[[[UIApplication sharedApplication] delegate] window];

as iWasRobbed pointed out but it wasn't working for me as the rootViewController property isn't reachable this way.

没︽人懂的悲伤 2024-09-18 12:40:37

试试这个,首先获取 UINavigationController 句柄,然后获取 topViewController

let navController = window?.rootViewController as! UINavigationController
let yourMainViewController = navController.topViewController as! ItemsViewController

let yourMainViewController = navController.viewControllers.first as! ItemsViewController

Try this, first get the UINavigationController handle, and then the topViewController

let navController = window?.rootViewController as! UINavigationController
let yourMainViewController = navController.topViewController as! ItemsViewController

or

let yourMainViewController = navController.viewControllers.first as! ItemsViewController
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文