iOS: different addSubview behavior between iOS 4.3 and 5.0

前端 未结 7 2028
长发绾君心
长发绾君心 2021-01-30 11:32

while coding in iOS 4.3 before, I found while add a view controller\'s view to another view with [superview addSubView:controller.view], the controller instance wil

相关标签:
7条回答
  • 2021-01-30 11:35

    It is iOS5 behavior:
    viewWillAppear, viewDidAppear, ... are executed automatically after addSubView: for iOS5.
    So for iOS5 no need to execute manually those methods as need for iOS<5.0.

    The fix may be:

    if ([[UIDevice currentDevice].systemVersion doubleValue] < 5.0) {
    ...execute viewWillAppear or other
    }
    
    0 讨论(0)
  • 2021-01-30 11:39

    The answers above a slightly incomplete. Let's presume you have 2 view controllers, ControllerA, and ControllerB.

    ControllerA.view is already added to the window(it is the parent), and you want to add ControllerB.view as a subview of ControllerA.

    If you do not add ControllerB as a child of ControllerA first, the automaticallyForwardAppearanceAndRotationMethodsToChildViewControllers will be ignored, and you will still be called by iOS5, meaning that you'll call your view controller callbacks twice.

    Example in ControllerA:

    - (BOOL)automaticallyForwardAppearanceAndRotationMethodsToChildViewControllers {
        return NO;
    }
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        self.controllerB = [[ControllerB alloc] initWithNibName:@"ControllerB" bundle:nil];
    
        [self.view addSubview:self.controllerB.view];
    }
    
    - (void)viewWillAppear:(BOOL)animated
    {
        [super viewWillAppear:animated];
        [self.controllerB viewWillAppear:animated];
    }
    

    In ControllerB NSLogging in viewWillAppear:

    - (void)viewWillAppear:(BOOL)animated
    {
        NSLog("@ControllerB will appear");
    }
    

    This will result in iOS5 only displaying that NSLog message twice. i.e. You're automaticallyForwardAppearanceAndRotationMethodsToChildViewControllers has been ignored.

    In order to fix this, you need to add controllerB as a child of controller a.

    Back in ControllerA's class:

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        self.controllerB = [[ControllerB alloc] initWithNibName:@"ControllerB" bundle:nil];
        if ([self respondsToSelector:@selector(addChildViewController:)])
            [self addChildViewController:self.controllerB];
    
        [self.view addSubview:self.controllerB.view];
    }
    

    This will now work as expected in both iOS4 and iOS5 without resorting to the horrible hack of checking iOS version strings, but instead checking on if the function we're after is available.

    Hope this helps.

    0 讨论(0)
  • 2021-01-30 11:39

    view{Will,Did}Appear, view{Will,Did}Disappear are functions on View Controllers and not views. These functions are called by SDK provided view controllers that are supposed to manage other view controllers e.g. UITabBarController, UINavigationBarController.

    If you are managing sub-view controllers yourself, you have to call these explicitly (and in proper order - though you should have a very good reason to do this). A modal view not getting these calls upon dismissal of a modal view is simply because there is no one there to call it. Encapsulate the root view controller in a UINavigationController (and hide the navigation bar if you like) and then open a modal view controller. Upon its dismissal, or pop, viewWillAppear will get called.

    0 讨论(0)
  • 2021-01-30 11:40

    In iOS 4 you had to manually call -viewWillAppear, -viewWillDisappear, etc. when adding or removing a view from your view hierarchy. These are called automatically in iOS 5 if the view is being added or removed from the window hierarchy. Fortunately, iOS 5 has a method in UIViewController that you can override to revert the behaviour back to how it worked with iOS 4. Just add this to your UIViewController:

    -(BOOL)automaticallyForwardAppearanceAndRotationMethodsToChildViewControllers {
       return NO;
    }
    

    This is probably the easiest solution as long as you're supporting both iOS 4 and iOS 5. Once you drop support for iOS 4 you might consider modifying your code to use the newer approach when swapping views.

    Edit 5 February 2012

    Apparently this function requires the child view controller be added to the main view controller using the addChildViewController: method. This method doesn't exist in iOS4, so you need to do something like this:

      if ([self respondsToSelector:@selector(addChildViewController:)] ) {
         [self addChildViewController:childViewController];
      }
    

    Thanks to everyone who corrected me on this.

    0 讨论(0)
  • 2021-01-30 11:50

    After reviewing all the evidence, I think the best thing to do is NOT use viewDidAppear etc for views that are affected by this ios 4 / ios 5 bug. Instead make a custom class (like viewDidAppearCustom) and call it yourself. this way you can guarantee that apple won't change the sdk again and mess you up. There is a great blog covering this issue here:

    http://gamesfromwithin.com/view-controller-notification-changes-on-ios5

    0 讨论(0)
  • 2021-01-30 11:51

    This may not be an answer what you want, but I had same kind of problem.

    In my case, when I added a view controller's view to another view controller's view as a subview, the subview was received viewWillAppear only in iOS 5.0 not iOS 4.X.

    So I added a nasty condition.

    [self.view addSubview:self.viewController.view];
    if ([[[UIDevice currentDevice] systemVersion] compare:@"5.0"] == NSOrderedAscending) {
        [self.viewController viewWillAppear:animated];
    }
    

    From iOS 5.0, Apple provides a way to implement custom container view controllers like UINavigationController or UITabController. I think this change affects when viewWillAppear is called.

    This problem may be solvable if we use -[UIViewController addChildViewController:].

    0 讨论(0)
提交回复
热议问题