Idiom for initializing UINavigationItem

Since UIViewController navigationItem outlets are deprecated (yes, I'm a little behind), what is the correct idiom defining UINavigationItem elements?

I saw several suggested approaches, but it’s completely not clear to me:

  • "Insert a navigation item into the view controller" in the XIB view controller.

  • Initialize the properties of the navigation element in the code with values ​​from scratch.

  • Create the navigation controller output in the view controller, connect the navigation element in the XIB view controller, and use its properties to initialize the properties of the (actual) navigation element in the code.

It is not clear to me how to “embed” a navigation element (just adding it as a child of the view controller in IB has no effect); and I don’t understand which of these approaches is better or, for that matter, where (how) to make 2 or 3.

+4
source share
2 answers

1) If the controller is created in XIB, you can reset the UINavigationItem to it and configure this element - it will work. For example, when you define a UINavigationControler in the XIB, you can put some controller inside as the root view controller. So you can have a UINavigationItem for this controller in XIB.

2) If the controller loads this view from the XIB (it was created by alloc and then init or initWithNibName:bundle: , it is presented in XIB only as a File Owner , which does not support UINavigationItem in it. In this case, you should configure the navigation element in the code (usually I do this in viewDidLoad ). No need to create it, it already exists in the navigtionItem property of your controller

 self.navigationItem.title = @"Price List"; 

It may be possible to make a way out for the navigation item, but I would not recommend this. Apple has declared such a release obsolete for a reason. I remember that I once discussed this with a colleague, but I forgot what it was (then it was obvious).

+7
source

In our project, we must make the UI as customizable as possible from IB. Thus, I add a UINavigationItem to xib, configure it, bind it as an output to my custom subclass of UIViewController, and then copy all properties at runtime using the method added to UIViewController using the category:

 - (void)setNavigationItemPropertiesFromOtherItem:(UINavigationItem *)navItem { // WORKAROUND: we can't link UINavigationItem to UIViewController from IB, and navigationItem property in UIViewController is readonly self.navigationItem.title = navItem.title; self.navigationItem.prompt = navItem.prompt; self.navigationItem.hidesBackButton = navItem.hidesBackButton; if (navItem.backBarButtonItem != nil) { self.navigationItem.backBarButtonItem = navItem.backBarButtonItem; } if (navItem.leftBarButtonItem != nil) { self.navigationItem.leftBarButtonItem = navItem.leftBarButtonItem; } if (navItem.rightBarButtonItem != nil) { self.navigationItem.rightBarButtonItem = navItem.rightBarButtonItem; } if (navItem.titleView != nil) { self.navigationItem.titleView = navItem.titleView; } } 

This workaround also allows you to associate button bar items with UINavigationItem using IB.

+1
source

Source: https://habr.com/ru/post/1343508/


All Articles