gpt4 book ai didi

ios - 在 iOS 中查找 rootViewController

转载 作者:技术小花猫 更新时间:2023-10-29 10:56:36 26 4
gpt4 key购买 nike

在 ShareKit 中,代码需要确定 rootViewController 的位置,以便它可以显示模态视图。由于某种原因,代码在 iOS 5 中失败:

    // Try to find the root view controller programmically

// Find the top window (that is not an alert view or other window)
UIWindow *topWindow = [[UIApplication sharedApplication] keyWindow];
if (topWindow.windowLevel != UIWindowLevelNormal)
{
NSArray *windows = [[UIApplication sharedApplication] windows];
for(topWindow in windows)
{
if (topWindow.windowLevel == UIWindowLevelNormal)
break;
}
}

UIView *rootView = [[topWindow subviews] objectAtIndex:0];
id nextResponder = [rootView nextResponder];

if ([nextResponder isKindOfClass:[UIViewController class]])
self.rootViewController = nextResponder;

else
NSAssert(NO, @"ShareKit: Could not find a root view controller. You can assign one manually by calling [[SHK currentHelper] setRootViewController:YOURROOTVIEWCONTROLLER].");

这是命中断言。

简单地使用以下代码有什么问题?

rootViewController = [[[UIApplication sharedApplication] keyWindow] rootViewController];

这似乎工作正常。它会在某些情况下失败吗?

最佳答案

我不确定你是否可以依赖 window.rootViewController b/c 你不必设置它。您可以只向窗口添加一个 subview 。以下似乎工作正常:

id rootVC = [[[[[UIApplication sharedApplication] keyWindow] subviews] objectAtIndex:0] nextResponder];

关于ios - 在 iOS 中查找 rootViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8585881/

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