Does adding a bar button item to the navigation bar programmatically when offering scene support differ from the old app delegate approach

I have used the following code for years to add a right bar button item to the navigation bar, but for some unknown reason, this no longer works. It stopped working when I updated my app to have Scene support. I don't understand what is preventing this code from working.


@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    NSLog(@"viewDidLoad");

    // Add a Share Button
    UIBarButtonItem *shareButton;
    shareButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(editProject:)];

    self.navigationItem.rightBarButtonItem = shareButton;
    
    self.navigationItem.rightBarButtonItem.tintColor = [UIColor blueColor];
}

-(void) editProject:(id)sender {

}
@end

To test this, I created a brand new test app that does nothing except for attempting to add this button. The autogenerated code gives you the following project and I simply modified the ViewController class as shown above:

What do I need to do differently to make the right bar button item to display? I know that I can add buttons using the storyboard that can be controlled via IBOutlets, but just want to know if its still possible to do this programmatically.

It doesn’t look like the view controller is contained within a navigation controller in the storyboard, if you add one it should work. I don’t believe scene lifecycle should affect anything here, unless you are setting up your bar button items in certain application lifecycle methods.

Thanks, but my original app did not have a navigation controller either and it worked until I upgraded for scene lifecycle support. Was I just lucky that it worked before or am I still missing something? Here is the header for my original code:

@interface Summary : UIViewController <UITableViewDelegate, UITableViewDataSource, UIPrintInteractionControllerDelegate, UIActionSheetDelegate, MFMailComposeViewControllerDelegate> {

I set the bar button item up in the viewDidLoad method (same code as in my post for the test app).

Accepted Answer

Ok, now I understand. In my original project, I mistakenly made my root view controller be the initial controller when I was upgrading to a scene lifecycle. I did have a navigation controller, but when I made my root view controller the initial controller, it removed the initial controller status from the navigation controller. That prevented the navigation bar item from working. So in this test project, once I added a navigation controller (as you suggested) and made the ViewController have a "root view controller" relationship to the navigation controller, then it works. Thanks.

Does adding a bar button item to the navigation bar programmatically when offering scene support differ from the old app delegate approach
 
 
Q