What is the delegate method called when rightBarButtonItem of UIBarButtonItem is tapped? I want to implement a specific action there.
There is no predefined delegate method. You need to set the delegate/action (similar to a UIControl). For example, create the UIBarButtonItem in viewDidLoad the following way:
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:#"Mark" style:UIBarButtonItemStylePlain target:self action:#selector(actionForTap:) autorelease];
and implement actionForTap: in your view controller. If you already have the UIBarButtonItem you can set the target/action to those of your desired delegate method, e.g.:
self.navigationItem.rightBarButtonItem.target = self;
self.navigationItem.rightBarButtonItem.action = #selector(actionForTap:);
As a third way, you can configure it in IB (but I'm not going there).
You don't need any delegate method. You can simply use the below code.
This is code for simply adding a button on the right and adding an action to that button.
In my case I called the method named "AddButtonMethod".
UIBarButtonItem *AddButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(AddButtonMethod:)];
self.navigationItem.rightBarButtonItem = AddButton;
Related
How to set an IBAction for the edit button in the NavigationBar created using the following code
self.navigationItem.leftBarButtonItem = [self editButtonItem];
Since you are assigning the left bar button programmatically, you must create the button yourself. When you instantiating the button, you can also set the callback action something like this:
UIBarButtonItem *editButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemEdit
target:self
action:#selector(editButtonPressed:)];
self.navigationItem.leftBarButtonItem = editButton;
When the edit button is pressed, the following method will be called
- (void)editButtonPressed:(id)sender
{
// do something
}
Since you are already working in code, rather than Interface Builder, it's probably easier to assign a selector programmatically as well:
[self.navigationItem.leftBarButtonItem setAction:#selector(doSomething:)];
I had some problem, i need to add an UIBarButtonItem who call a method named "valider"
- (void) valider:(NSString*) firstParam
{
NSLog(#"Logs %#", firstParam);
}
and i creat my UIBarButtonItem like this, but i need to pass my first parameter !
UIBarButtonItem *buttonValiderAddSerie = [[UIBarButtonItem alloc]initWithTitle:#"Valider" style:UIBarButtonItemStyleBordered target:self action:#selector(valider:)];
I tried this but it doesn't work for me...
UIBarButtonItem *buttonValiderAddSerie = [[UIBarButtonItem alloc]initWithTitle:#"Valider" style:UIBarButtonItemStyleBordered
[buttonValidAddSerie addTarget:self action:#selector(valider:) firstParam:#"first"];
Thank's for reading,
Tommy
UIButtonBarItems actions signature look like
- (void)didTapButtonBarItem:(id)sender;
The sender parameter is a pointer to the button currently pressed.
You have to store your first parameter elsewhere in your class, then retrieve it when you tap on buttonValidAddSerie.
I have UItable with items. I have navigation bar etc. On one item in table "Category" I pust another UITable like this:
CategoryTableViewController *b = [[CategoryTableViewController alloc]init];
[self.navigationController pushViewController:b animated:YES];
[b release];
Now I want to add "ADD" button in navigation bar and I add UINavigationBarItem in *.xib connect it to outlets and add it like this in viewDidLoad:
self.navigationItem.rightBarButtonItem = self.addButton;
And this does not work (addButton is null), but when I put the same code for adding button in my first UITable it works fine and "ADD" button is added.
What could be the problem here?
In the ViewController that should show the button in the navigation bar type in the viewDidLoad() method:
self.addToolbarButton = [[[UIBarButtonItem alloc]
initWithTitle:#"Add", nil)
style:UIBarButtonSystemItemAdd
target:self
action:#selector (add)] autorelease];
self.navigationItem.leftBarButtonItem = addToolbarButton;
That will add a "add" styled button to the left in the navigation bar which calls the selector method:
-(void) add {...}
in the same class when it is tapped. In this method in the same class you can specify your add logic. If this method should be placed in a different class, set the target to that.
That is the programatical way to solve this. The method "-(void) add" is what your Outlet has been in the .xib approach.
For your .xib approach you should verify that the Outlet property for the navigationBarButton is set to retain.
self.addButton is NULL, thus make sure its not NULL. Create a button from code.
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(doneMeasuring:)];
is [super viewDidLoad]; the first call in your viewDidLoad method?
If not try to put it at the very beginning of viewDidLoad.
How can a button be added to a navigation toolbar in an application which is navigation based?
The rootController is a tableView by default, and this is perfect. I would like to add a "share" button to the navigation controller so that I can attach it to my own method.
How is this done if the navigation bar is added in the code somewhere?
To do it in code, go to your views viewDidLoad method and create a UIBarButtonItem
This code will create a button that says share like you wanted and put it on the right hand side of the navigation bar.
- (void)viewDidLoad {
[super viewDidLoad];
UIBarButtonItem *shareButton = [[UIBarButtonItem alloc] initWithTitle:#"Share" style:UIBarButtonItemStyleBordered target:self action:#selector(share)];
self.navigationItem.rightBarButtonItem = shareButton;
[shareButton release];
}
UIBarButtonItem *shareButton = [[[UIBarButtonItem alloc] initWithTitle:#"Share" style:UIBarButtonItemStylePlain target:self action:#selector(yourShareAction)] autorelease];
self.navigationItem.rightBarButtonItem = shareButton;
This makes an add button, but you get the idea.
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:
self action:#selector(buttonPressed:)]
autorelease];
You can create an UIBarButtonItem and attach it to a root view controller's navigation item. You add a target-action pair when you initialize the UIBarButtonItem object and that action message will be sent to the target on a user tapping on the button.
So if you want the button when the table view is shown, set the UIBarButtonItem to your table view controller's navigationItem.rightBarButtomItem property.
UIBarButtonItem *rButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:NULL];
rButton.action = #selector(refreshAction);
self.navigationItem.rightBarButtonItem = rButton;
[rButton release];
The above code works fine to create a button and add it to the navigation bar, but when I click on the button it crashes the app with a EXC_BAD_ACCESS. If I comment out rButton.action = #selector(refreshAction); clicking the button won't do anything but it doesn't crash either.
- (void)refreshAction {
NSLog(#"refreshAction");
}
This code is in a TableViewController's viewDidLoad method which is pushed onto the navigationController stack from the NavigationViewController viewDidLoad method.
I've spent probably 3 hours trying to get this to work, to no avail.
As usual memory management was the culprit. Loading the tableViewController from the navigationController:
NearbyTableViewController *tableController = [[[NearbyTableViewController alloc] initWithNibName:#"NearbyTableViewController" bundle:nil] autorelease];
self.nearbyTableController = tableController;
[self pushViewController:self.nearbyTableController animated:YES];
[tableController release];
releasing an object set to autorelease... must be the most common error in memory management.
Deleting that line [tableController release] solved the problem
Any luck if you specify #selector(refreshAction) when you create the button, i.e.:
UIBarButtonItem *rButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:#selector(refreshAction)];
Maybe the target doesn't get saved if you don't also specify the action to the initializer.
I'm not exactly 100% sure why your code does not work, but setting the selector directly in the constructor does work:
UIBarButtonItem *rButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh
target:self
action:#selector(refreshAction)];