LoginSignup
2
2

More than 5 years have passed since last update.

iOS5でviewDidAppear:内でaddChildViewController:するとChildViewControllerのviewWillAppear:が呼ばれない

Posted at

なにこれ……

AppDelegate.h
//
//  AppDelegate.m
//  SimpleNestedViewController

#import "AppDelegate.h"

@interface ChildViewController : UIViewController
@end

@implementation ChildViewController

- (void)viewWillAppear:(BOOL)animated
{
  [super viewWillAppear:animated];

  NSLog(@"iOS5だとviewWillAppear:が呼ばれない");
}

@end

@interface ParentViewController : UIViewController
@end

@implementation ParentViewController

- (void)viewDidAppear:(BOOL)animated
{
  [super viewDidAppear:animated];

  ChildViewController* childViewController = [ChildViewController new];
  [self addChildViewController:childViewController];
  [self.view addSubview:childViewController.view];
  [childViewController didMoveToParentViewController:self];

  dispatch_async(dispatch_get_main_queue(), ^{
    // ここに移動するとNSLog呼ばれる
  });
}

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
  self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
  self.window.rootViewController = [ParentViewController new];
  [self.window makeKeyAndVisible];
  return YES;
}

@end

(iOS SDK 7.1 / iOS 5.1.1 で確認)

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2