3

Starting from the spliview master/detail example I found in the SDK, I end up having an iPad app that has a master on the left hand side in landscape, whereas in portrait a button called "Master" allows displaying the master.

Landscape Master

Portrait Master

To display this button the following code is used

- (void)splitViewController:(UISplitViewController *)splitController willHideViewController:(UIViewController *)viewController withBarButtonItem:(UIBarButtonItem *)barButtonItem forPopoverController:(UIPopoverController *)popoverController
{
    barButtonItem.title = @"Master";
    [self.navigationItem setLeftBarButtonItem:barButtonItem animated:YES];
    self.masterPopoverController = popoverController;
}

In my app the master view will be some kind of search view, so I would like the button called master to be displaying the system provided search icon.

First of all I simply do not know how to programmatically (at runtime) add a system icon to a button ? I know how to select it in the storyboard in Xcode at development time but not at runtime.

As I don't know how to do it, for testing purpose I add a right bar button on the top of the bar and I simply assign its image to the master button, like this :

- (void)splitViewController:(UISplitViewController *)splitController willHideViewController:(UIViewController *)viewController withBarButtonItem:(UIBarButtonItem *)barButtonItem forPopoverController:(UIPopoverController *)popoverController
{
    //barButtonItem.title = @"Master";
    barButtonItem.image = searchBarButton.image;
    [self.navigationItem setLeftBarButtonItem:barButtonItem animated:YES];
    self.masterPopoverController = popoverController;
}

And the result is that the master button simply does not display.

enter image description here

I repeat, this is just for testing purpose, finally I would like to simply assign the system provided search icon.

So my 2 questions are :

  1. Why is the master button not displaying ? Am I obligated to use "text" for this button
  2. How do I assign a system provided icon (at runtime) to such a button ?

Anyone can help me ?

HpTerm
  • 7,871
  • 12
  • 48
  • 67

1 Answers1

2

You need to initialize the bar button item. You can do this using the following code, replacing the target and action parameters with your target and function.

self.barButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Master" style:UIBarButtonItemStylePlain target:self action:@selector(action)];
[self.navigationItem setLeftBarButtonItem:_barButtonItem animated:YES];

If you want to use a system item, then use the following code, replacing initWithTitle with initWithBarButtonSystemItem. Once again, replace the target and action parameters.

self.barButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(action)];
[self.navigationItem setLeftBarButtonItem:_barButtonItem animated:YES];

You also have to make sure you include the barButtonItem under @interface.

@interface ViewController ()
@property UIBarButtonItem *barButtonItem;
@end
trumpeter201
  • 8,087
  • 3
  • 15
  • 24