开发者

Which UIViewController properties should I set in -initWithNibName:bundle: vs. -viewDidLoad?

开发者 https://www.devze.com 2023-04-02 15:58 出处:网络
I understand that I should set self.title in 开发者_开发百科-initWithNibName:bundle:. What about self.navigationItem.titleView?

I understand that I should set self.title in 开发者_开发百科-initWithNibName:bundle:.

  1. What about self.navigationItem.titleView?

    Since self.navigationItem.titleView seems only to be used when self.view is loaded, I'm thinking I should, to save memory, set self.navigationItem.titleView in -viewDidLoad and nil it in -viewDidUnload, e.g.:

    - (void)viewDidLoad {
        [super viewDidLoad];
        self.navigationItem.titleView = [[UIImageView alloc] initWithImage:
                                         [UIImage imageNamed:@"logo.png"]];
    }
    
    - (void)viewDidUnload {
        self.navigationItem.titleView = nil;
        [super viewDidUnload];
    }
    
  2. What about self.navigationItem.backBarButtonItem?


  1. It seems to work OK to set self.navigationItem.titleView in -viewDidLoad and nil it in -viewDidUnload.

  2. You should set self.navigationItem.backBarButtonItem in -initWithNibName:bundle because if you push two view controllers without animation -viewDidLoad will not get called for the first view controller that's pushed. So, if that view controller sets self.navigationItem.backBarButtonItem in -viewDidLoad, it will actually not get set, and the back button on the second view controller will just default to the title of the first view controller as usual.

0

精彩评论

暂无评论...
验证码 换一张
取 消