gpt4 book ai didi

ios - 递归调用viewDidLoad,崩溃

转载 作者:行者123 更新时间:2023-11-29 04:17:13 24 4
gpt4 key购买 nike

在loggerViewController.m中:

- (void)viewDidLoad
{
[super viewDidLoad];
UIView* mainView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
[self.view addSubview:mainView]; // <-- Problem is here
}

loggingViewController 是我的 appDelegate 的 iVar

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
.
.
loggingViewController = [[loggerViewController alloc] init];
[loggingViewController.view setBackgroundColor:[UIColor blueColor]];
// [loggingViewController loadView];
[self.view addSubview:loggingViewController.view];

}

我期待我的 AppDelegate 调用loggingViewController,而loggingViewController又会在内部设置它自己的 subview ,然后就完成了。但是 viewDidLoad 被递归调用,我不明白为什么?

最佳答案

像这样尝试一下,

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
loggingViewController = [[loggerViewController alloc] init];
self.window.rootViewController = loggingViewController;
[self.window makeKeyAndVisible];
}

递归调用的原因是您的 self.view 为 nil,因此当您尝试将其添加为 subview 时,它会尝试一次又一次地调用appdelegate 的 View 。

- (void)viewDidLoad
{
[super viewDidLoad];
UIView* mainView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
[self.view setBackgroundColor:[UIColor blueColor]];
[self.view addSubview:mainView];
}

关于ios - 递归调用viewDidLoad,崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13521825/

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