Welcome, Guest
Username Password: Remember me

Detail View Controller - Button not showing up
(1 viewing) (1) Guest
  • Page:
  • 1
  • 2

TOPIC: Detail View Controller - Button not showing up

Detail View Controller - Button not showing up 1 year, 4 months ago #1

Afternoon

In the iPad App example that comes with STV, if I go to the detail view, there are no buttons shown as they are generated by the code.

However, I wanted to add a settings button to allow my new PinLockController to work.

At the end, everything built ok, but there was no button, so have been trying to figure out what I did wrong.

I ended seeing if I could move the button - I moved it to the bottom of the DetailViewController - then when I built it, it showed up and worked with no issues, except this is not where I want it.

I would like to add it next to + button.

Any ideas as to how I can achieve this ?

Iain
  • ihmunro
  • OFFLINE
  • Expert Boarder
  • Posts: 135
  • Karma: 0

Re: Detail View Controller - Button not showing up 1 year, 4 months ago #2

Hi again,

The navigation bar allows only one button to the right and one to the left. However, you can create a toolbar with buttons and have it as the view of a UIBarButtonItem. Here is some sample code to put in DetailViewController.m inside viewDidLoad (replace existing code):

 
// Create the toolbar that will hold the Add and Edit buttons
UIToolbar* toolbar = [[UIToolbar alloc]
initWithFrame:CGRectMake(0, 0, 100, 44.01)];
 
// Set the toolbar style to the correct style
toolbar.barStyle = self.navigationController.navigationBar.barStyle;
 
// create an array of the buttons
NSMutableArray* buttons = [[NSMutableArray alloc] initWithCapacity:3];
 
UIBarButtonItem *_addButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self
action:@selector(addButtonTapped)];
self.addButtonItem = _addButton;
[_addButton release];
[buttons addObject:self.addButtonItem];
UIBarButtonItem *spacer = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
target:nil action:nil];
[buttons addObject:spacer];
[spacer release];
UIBarButtonItem *settingsButton = [[UIBarButtonItem alloc]
initWithTitle:@"Settings" style:UIBarButtonItemStyleBordered
target:self action:@selector(settingsButtonTapped)]; // Assumes you have a method called settingsButtonTapped
[buttons addObject:settingsButton];
[settingsButton release];
 
// add the buttons to the toolbar
[toolbar setItems:buttons animated:NO];
[buttons release];
 
// place the toolbar into the navigation bar
UIBarButtonItem *toolbarButton = [[UIBarButtonItem alloc] initWithCustomView:toolbar];
self.navigationItem.rightBarButtonItem = toolbarButton;
[toolbar release];
[toolbarButton release];
 
  • tarekskr
  • OFFLINE
  • Administrator
  • Posts: 2402
  • Karma: 72
Last Edit: 1 year, 4 months ago by tarekskr. Reason: Fixed a typo

Re: Detail View Controller - Button not showing up 1 year, 4 months ago #3

Hi Tarek

Thanks for code.

I got one build error - not a structure or union

[buttons addObject:self.addButton];

Any ideas ?

Iain
  • ihmunro
  • OFFLINE
  • Expert Boarder
  • Posts: 135
  • Karma: 0

Re: Detail View Controller - Button not showing up 1 year, 4 months ago #4

Sorry for the typo, this should be [buttons addObject:self.addButtonItem]. I also fixed the original post.
  • tarekskr
  • OFFLINE
  • Administrator
  • Posts: 2402
  • Karma: 72

Re: Detail View Controller - Button not showing up 1 year, 4 months ago #5

Hi Tarek

Thanks - dont worry about the typo - you have only made one mistake in the history of STV - you should see how many I have made

I added in the code and it now runs - the settings button shows, but the app terminates.

I changed the code to:

// Create the toolbar that will hold the Add and Edit buttons
UIToolbar* toolbar = [UIToolbar alloc]
initWithFrame:CGRectMake(0, 0, 100, 44.01)];

// Set the toolbar style to the correct style
toolbar.barStyle = self.navigationController.navigationBar.barStyle;

// create an array of the buttons
NSMutableArray* buttons = [NSMutableArray alloc] initWithCapacity:3];

UIBarButtonItem *_addButton = [UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self
action:@selector(addButtonTapped)];
self.addButtonItem = _addButton;
[_addButton release];
[buttons addObject:self.addButtonItem];
UIBarButtonItem *spacer = [UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
target:nil action:nil];
[buttons addObject:spacer];
[spacer release];
UIBarButtonItem *showSettings = [UIBarButtonItem alloc]
initWithTitle:@"Settings" style:UIBarButtonItemStyleBordered
target:self action:@selector(showSettings)]; // Assumes you have a method called settingsButtonTapped
[buttons addObject:showSettings];
[showSettings release];

// add the buttons to the toolbar
[toolbar setItems:buttons animated:NO];
[buttons release];

// place the toolbar into the navigation bar
UIBarButtonItem *toolbarButton = [UIBarButtonItem alloc] initWithCustomView:toolbar];
self.navigationItem.rightBarButtonItem = toolbarButton;
[toolbar release];
[toolbarButton release];


I have the following method called showSettings set up:

- (IBAction)showSettings:(id)sender {
[popoverController dismissPopoverAnimated:YES];

if (!settingsPopoverController){
SettingsViewController * settings=[SettingsViewController alloc] initWithStyle:UITableViewStyleGrouped];
settingsPopoverController = [UIPopoverController alloc] initWithContentViewController:settings];


[settings release];
};

if(! settingsPopoverController.popoverVisible){
[settingsPopoverController presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];}
else {
[settingsPopoverController dismissPopoverAnimated:YES];

}


Any ideas why it is terminating ?


Iain
  • ihmunro
  • OFFLINE
  • Expert Boarder
  • Posts: 135
  • Karma: 0

Re: Detail View Controller - Button not showing up 1 year, 4 months ago #6


Thanks - dont worry about the typo - you have only made one mistake in the history of STV


Really wish that was true!!

Now back to the code...

Your problem is that "@selector(showSettings)" refers to a method that has no parameters, while your "showSettings" declaration does have parameters. A correct declaration should look something like this:

 
- (void)showSettings
{
...
}
 
  • tarekskr
  • OFFLINE
  • Administrator
  • Posts: 2402
  • Karma: 72
  • Page:
  • 1
  • 2
Time to create page: 1.06 seconds