0

在 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,然后它会在里面设置它自己的子视图并且它会完成。但是相反, viewDidLoad 被递归调用,我不明白为什么?

4

1 回答 1

1

试试这样

- (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.viewis nil,因此当您尝试将其添加为 appdelegate 视图的子视图时,它会一次又一次地尝试调用。

- (void)viewDidLoad
{
    [super viewDidLoad];
    UIView* mainView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    [self.view setBackgroundColor:[UIColor blueColor]];
    [self.view addSubview:mainView];
}
于 2012-11-23T01:01:47.757 回答