UA-17470720-3

Jump to content


Photo
- - - - -

Detail View Controller - Button not showing up


  • Please log in to reply
11 replies to this topic

#1 Iain Munro

Iain Munro

    Advanced Member

  • STV 3.0 Pro
  • PipPipPipPipPip
  • 135 posts
Reputation: 2
Good

Posted 09 January 2011 - 06:20 PM

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

#2 Tarek

Tarek

    Forum Admin

  • Administrators
  • 3670 posts
Reputation: 452
Popular

Posted 09 January 2011 - 11:40 PM

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];


#3 Iain Munro

Iain Munro

    Advanced Member

  • STV 3.0 Pro
  • PipPipPipPipPip
  • 135 posts
Reputation: 2
Good

Posted 10 January 2011 - 11:05 AM

Hi Tarek

Thanks for code.

I got one build error - not a structure or union

[buttons addObject:self.addButton];

Any ideas ?

Iain

#4 Tarek

Tarek

    Forum Admin

  • Administrators
  • 3670 posts
Reputation: 452
Popular

Posted 10 January 2011 - 12:31 PM

Sorry for the typo, this should be [buttons addObject:self.addButtonItem]. I also fixed the original post.

#5 Iain Munro

Iain Munro

    Advanced Member

  • STV 3.0 Pro
  • PipPipPipPipPip
  • 135 posts
Reputation: 2
Good

Posted 10 January 2011 - 01:11 PM

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 :laugh:

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

#6 Tarek

Tarek

    Forum Admin

  • Administrators
  • 3670 posts
Reputation: 452
Popular

Posted 11 January 2011 - 10:16 AM

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
{
  ...
}


#7 Iain Munro

Iain Munro

    Advanced Member

  • STV 3.0 Pro
  • PipPipPipPipPip
  • 135 posts
Reputation: 2
Good

Posted 12 January 2011 - 02:49 PM

Hi Tarek

I am not sure what to do as the following was setup to get my pin lock controller to work:

- (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];

}

}

I did try what you suggested before this, but nothing happened. When I wrapped it with curly brackets, I got an error code.

Any ideas ?

Iain

#8 Tarek

Tarek

    Forum Admin

  • Administrators
  • 3670 posts
Reputation: 452
Popular

Posted 12 January 2011 - 11:17 PM

Iain,

You're still declaring your method incorrectly, it should be:

- (void)showSettings
{
  ...
}

You also have several syntactical errors that are outside the scope of this forum.

#9 David DelMonte

David DelMonte

    Forum Master

  • STV 4.0 Pro
  • PipPipPipPipPipPipPip
  • 527 posts
Reputation: 41
Excellent

Posted 13 April 2011 - 07:50 PM

Forgive me for jumping into this thread. I am also trying to add a third button to the nav. bar.

I am able to see the edit button and my new button (cards), but I've lost the add button :S

Here's the button code that I have:

	// 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];
	
	// Set up the edit and add buttons.
	self.navigationItem.leftBarButtonItem = self.editButtonItem;
	
	
	UIBarButtonItem *spacer = [[UIBarButtonItem alloc]
							   initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
							   target:nil action:nil];
	[buttons addObject:spacer];
	[spacer release];
	
	UIBarButtonItem *flipButton = [[UIBarButtonItem alloc] 
								   initWithTitle:@"Cards" style:UIBarButtonItemStyleBordered 
								   target:self action:@selector(flipToFront)]; 
	[buttons addObject:flipButton];
	[flipButton release];
	
	UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:nil action:nil];
	self.navigationItem.rightBarButtonItem = addButton;
	[addButton 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've tried some other permutations of course, but I thought I should ask again for some help..

#10 David DelMonte

David DelMonte

    Forum Master

  • STV 4.0 Pro
  • PipPipPipPipPipPipPip
  • 527 posts
Reputation: 41
Excellent

Posted 13 April 2011 - 08:12 PM

I have modified the code a bit, and can see the + now. However, it is not active.

I can see that there are two items named "rightBarButtonItems" but I don't know what to do about them :silly:

Here's the modded 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:4];
	
	// Set up the edit and add buttons.
	self.navigationItem.leftBarButtonItem = self.editButtonItem;
	
	
	UIBarButtonItem *spacer = [[UIBarButtonItem alloc]
							   initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
							   target:nil action:nil];
	[buttons addObject:spacer];
	[spacer release];
	
	UIBarButtonItem *flipButton = [[UIBarButtonItem alloc] 
								   initWithTitle:@"Cards" style:UIBarButtonItemStyleBordered 
								   target:self action:@selector(flipToFront)]; 
	[buttons addObject:flipButton];
	[flipButton release];
	
	UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:nil action:nil];
	//self.navigationItem.rightBarButtonItem = addButton;
	[buttons addObject:addButton];
	[addButton 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];
	

Thanks for any help..

#11 David DelMonte

David DelMonte

    Forum Master

  • STV 4.0 Pro
  • PipPipPipPipPipPipPip
  • 527 posts
Reputation: 41
Excellent

Posted 14 April 2011 - 08:20 AM

Digging deeper, has there been a change in STV 2.x.

I look at the example files and see this code:

 // Set up the edit and add buttons.
	self.navigationItem.leftBarButtonItem = self.editButtonItem;
	
	UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(insertNewObject)];
	self.navigationItem.rightBarButtonItem = addButton;
	[addButton release];

When I try to use insertNewObject, I get unknown selector..

#12 Tarek

Tarek

    Forum Admin

  • Administrators
  • 3670 posts
Reputation: 452
Popular

Posted 14 April 2011 - 10:32 AM

Hi David,

I have a feeling that your issue has to do with the actual width of the UIToolBar, have you tried increasing that? Also, since this is not an STV issue, posting about it in StackOverflow (for example) will give you access to a much wider expert base than here. Good luck!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users