UIBarButtonItem action with parameter - iphone

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.

Related

Why can't I change UIBarButtonItem's title?

I want to change UIBarButtonItem's title.
But this code is not working.
- (void)viewDidLoad {
...
[self smay];
}
- (void)smay {
...
AppDelegate *apd = (AppDelegate*)[[UIApplication sharedApplication]delegate];
NSString *SmayUse = [NSString stringWithFormat:#"%d月%d日",
apd.newyear,apd.newmonth];
[_smay setTitle:SmayUse]
}
I don't know how to solve this problem.
Please tell me a way to solve this problem.
Initialize your UIBarButtonItem like this:
_smay = [[UIBarButtonItem alloc] initWithTitle:#"Your Title"
style:UIBarButtonItemStyleBordered
target:self action:#selector(yourMethod)];
self.navigationItem.rightBarButtonItem = _smay;
Then, if you want to change the title somewhere else in your code:
[_smay setTitle:#"Your Other Title"];
IOS 8 and higher
UIBarButtonItemStyleBordered is deprecated, so use the below.
[[UIBarButtonItem alloc] initWithTitle:#"Your Title"
style:UIBarButtonItemStylePlain
target:self action:#selector(yourMethod)];
if u want to change the title of navigation bar programmatically means
UIBarButtonItem *aDoneItem = [UIBarButtonItem alloc]initWithTitle:#"Done" style:UIBarButtonItemStyleBordered
target:self
action:#selector(toggleEdit:)];
navigationController.rightBarButtonItem = aDoneItem;
aDoneItem.title = #"Something";
You have to make sure the button is initialized. If it is part of a .xib file, make sure you have it linked up with your #property that has also an IBOutlet associated with it. Otherwise you need to initialize it programatically. Also, you should use the method setTitle:forState:.
item.title = #"new title" should work.

Set IBAction for edit button in NavigationBar

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

Delegate method called for rightBarButtonItem of UIBarButtonItem

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;

How can I modify the target of the default Back button on iphone?

I want to have the default shape of the back button, but that makes my program enter a loop at some point.
The code is this for modifying the text of the back button, written before the initialization of the view.
UIBarButtonItem *newBackButton = [[UIBarButtonItem alloc] initWithTitle: #"Back" style: UIBarButtonItemStyleBordered target: nil action: nil];
[[self navigationItem] setBackBarButtonItem: newBackButton];
[newBackButton release];
can I also change the target to another view? I really need that default shape and I don't know how else to get it. Thanks!
The UINavigationController does not work that way, you'll need to make a custom UIBarButton with an image.
If you want to customize the right-button in the navigation controller you usually set the backBarButtonItem on the parent view controller, i.e. the one you are going back to.
parentViewController.navigationItem.backBarButtonItem =
[[[UIBarButtonItem alloc] initWithTitle:#"Back"
style:UIBarButtonItemStyleBordered
target:nil action:nil] autorelease];
There is a solution to modify the target of the back button on this blog. The key idea is to subclass UINavigationController and override the popViewControllerAnimated: method. First paste in your custom code and then call [super popViewControllerAnimated:animated] at the end.
I have verified that this still works fine in SDK 4.3.

How to add multiple buttons to a NavigationBar? [duplicate]

This question already has answers here:
How do you add more than one UIBarButton on UINavigationItem.rightBarButtonItem (or leftBarButtonItem)?
(8 answers)
Closed 9 years ago.
Ok, so I've successfully added a rightBarButtonItem to call a custom selector (called from a UIViewController), as follows:
UIButton* infoButton = [UIButton buttonWithType:UIButtonTypeInfoLight];
[infoButton addTarget:self action:#selector(showInfoView:) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:infoButton];
Is it possible to make the "right bar button item" actually be several distinct buttons (and thus remove the title text from the navigation bar)?
I'm searching for ways to get more screen real estate, and this seems like a logical choice... although any other suggestions would be appreciated (maybe i should reduce the size of the tab bar i use at the bottom...)
UIBarButtonItem *addAcc = [[UIBarButtonItem alloc]
initWithTitle:#"Add"
style:UIBarButtonItemStylePlain
target:self
action:#selector(addNewAcc)];
UIBarButtonItem *delAcc = [[UIBarButtonItem alloc]
initWithTitle:#"Del"
style:UIBarButtonItemStylePlain
target:self
action:#selector(DeleteButtonAction)];
NSArray *arrBtns = [[NSArray alloc]initWithObjects:addAcc,delAcc, nil];
self.navigationItem.rightBarButtonItems = arrBtns;
Well, you could use a UISegmentedControl with the bar style. Or you could add a generic UIView and add whatever buttons you want.