why does this different “addSubView” code cause differences in behavior

前端 未结 1 1434
情话喂你
情话喂你 2021-02-15 17:53

Why does changing the below code from the Old to New entry fix the following problem.

Code:

  // OLD Entry - Did not work
  //[self.window addSubview:na         


        
相关标签:
1条回答
  • 2021-02-15 18:46

    The difference between the two methods is that one triggers the view lifecycle methods the other dose not.

    I.e. setting window.rootViewController will cause the old view controller to receive the messages: viewDidDissaper viewWillDissapear etc.. while the new view controller will receive the viewWillApear, viewDidAppear etc..

    addSubview: does not do this.

    Does this help?

    EDIT:

    Reading your post in detail it looks like you are adding the buttons programatically on the viewDidAppear method of the detail view.

    The rootViewController property of UIWindow was added as of 4.0. The documentation does not explicitly mention that it will trigger the view life cycle methods, i found this out through trail and error like yourself. (perhaps someone can raise an issue against the apple documentation).

    If you need to be backwards compatible for 3.x you can this a custom UIWindow subclass. My code is below. use window.djRootViewController = yourViewController instead of window.rootViewController. It is designed for use in Interface builder.

    #import <UIKit/UIKit.h>
    
    @interface DJWindow : UIWindow {
    
        UINavigationController* m_navigationController;
    }
    
    @property (nonatomic, retain) UIViewController* djRootViewController;
    
    @end
    
    
    
    
    #import "DJWindow.h"
    
    @interface DJWindow()
    
    - (void) customInit;
    
    @end
    
    
    @implementation DJWindow
    
    
    - (id) initWithCoder:(NSCoder *)coder {
        self = [super initWithCoder:coder];
        if (self) {
            [self customInit];
        }
        return self;
    }
    
    
    - (void) customInit {
        m_navigationController = [[UINavigationController alloc] init];
    
        m_navigationController.navigationBarHidden = YES;
    
        [self addSubview:m_navigationController.view];
    }
    
    - (void) setRootViewController:(UIViewController *)rootViewController {
        NSLog(@"ERROR, do not set the rootViewController property, use djRootViewController instead");
    }
    
    - (void) setDjRootViewController:(UIViewController *)djRootViewController {
    
        if (djRootViewController == nil) {
            [m_navigationController setViewControllers:nil];
        } else {
            NSArray* vcArray = [NSArray arrayWithObject:djRootViewController];
            [m_navigationController setViewControllers:vcArray];
        }
    }
    
    - (UIViewController*) djRootViewController {
    
        return m_navigationController.visibleViewController;
    }
    
    - (void)dealloc
    {
        [m_navigationController release];
        [super dealloc];
    }
    
    
    @end
    
    0 讨论(0)
提交回复
热议问题