質問

I am currently trying to add a navigation bar for every screen. However I want to programmatically add different types of buttons available on different screens, specifically on the different slide drawer menu items. Also, I want to be able to have a back button for nested screens.

I have read about the navigation controller and I see there is just a navigation bar that I can add to my xib file, but not sure how to implement it exactly. If I add a navigation bar to the xib file do I have to reference it somehow in my controller or should adding it be enough, because adding this line does not add the items to the navigation bar.

This is the code I add in my MainViewController.m file:

//add navigation top bar items
        UIBarButtonItem *shareItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:nil];
        UIBarButtonItem *cameraItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:nil];

        NSArray *actionButtonItems = @[shareItem, cameraItem];
        self.navigationItem.rightBarButtonItems = actionButtonItems;
役に立ちましたか?

解決

Where ever you initially create your MainViewController (and most likely assign it to the windows rootViewController property) wrap this in a UINavigationController.

MainViewController *mainViewController = [MainViewController new];
self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:mainViewController];

You code in your question is then correct, and those UIBarButtonItems will then show in the navigation bar.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top