gpt4 book ai didi

ios - 从应用程序 :didFinishLaunching does not respond to touch events 中的 Storyboard手动创建的 View Controller

转载 作者:行者123 更新时间:2023-11-28 22:15:32 25 4
gpt4 key购买 nike

我想在代码中手动创建 Storyboard,而不是让它自动加载,所以我首先删除了 plist 中的启动 Storyboard,如下所示:

enter image description here

在我的应用委托(delegate)中使用这个:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UIStoryboard * storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];

UIViewController * initalViewController = [storyboard instantiateInitialViewController];

UIWindow *window = [[UIWindow alloc] init];
[window setRootViewController:initalViewController];
[window makeKeyAndVisible];

self.window = window;

return YES;
}

初始 View Controller 正确加载并可见,但它没有获得任何触摸事件。我什至在 touchesBegan 上设置了一个断点。有谁知道为什么没有传递触摸?

最佳答案

您创建的窗口没有框架,这意味着它实际上有一个框架 {{0, 0}, {0, 0}}。 View 无法接收超出其父边界的触摸,因此对于零帧,一切都在窗口边界之外。

你应该更新成这样

UIWindow *window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];

请参阅 UIWindow 中的注释文档(强调我自己的)

Note:
When you use storyboards and the Xcode app templates to create an app, a window is created for you. If you choose to create a window in Interface Builder, be sure to select the Full Screen at Launch option in the Attributes inspector so that the window is sized appropriately for the current device. Because a window doesn’t receive touch events outside of its bounds and views aren’t clipped to the window’s bounds by default, an improperly sized window might not be able to deliver touch events to all its views.

关于ios - 从应用程序 :didFinishLaunching does not respond to touch events 中的 Storyboard手动创建的 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21818041/

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