i try to customize the name of back button item. Do you know what is the method that should be in action ?
This is the code:
UIBarButtonItem * backButton = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStylePlain target:self action:??];
backButton.title = NSLocalizedString(#"backButtonNavigation", nil);
self.navigationItem.leftBarButtonItem = backButton;
Thanks in advance
Related
I'm trying to add Previous/Next buttons above the keyboard like in mobile Safari. There are a lot of questions about that here on StackOverflow, but most answers are to use inputAccessoryView.
I tried that and it looks like this:
Is there anyway to have the buttons in the toolbar bellow, like it works in mobile Safari??
This is how it looks like in Safari:
It seems what I was looking for is inputAssistantItem.
It allows you to add and change buttons in the keyboard's shortcut bar.
UIBarButtonItem* nextButton = [[UIBarButtonItem alloc]
initWithImage:nextImage
style:UIBarButtonItemStylePlain
target:self
action:#selector(nextField:)];
UIBarButtonItem* previousButton = [[UIBarButtonItem alloc]
initWithImage:previousImage
style:UIBarButtonItemStylePlain
target:self
action:#selector(previousField:)];
NSArray *buttonGroups = #[[[UIBarButtonItemGroup alloc]
initWithBarButtonItems:#[previousButton, nextButton]
representativeItem:nil]];
textField.inputAssistantItem.trailingBarButtonGroups = buttonGroups;
UIBarButtonItem *previous = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:103 target:self action:#selector(previousButtonTapped:)];
UIBarButtonItem *fixedSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
[fixedSpace setWidth:6.0];
UIBarButtonItem *next = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:104 target:self action:#selector(nextButtonTapped:)];
I am trying to call the UIBarButtonItem when the back button of that controller is pressed.This is my code.I have put the initialization in viewDidLoad and viewWillAppear but the method is not being called.
UIBarButtonItem *exampleButton = [[UIBarButtonItem alloc] initWithTitle:#" " style:UIBarButtonItemStylePlain target:self action:(#selector(backToViewController:))];
self.navigationItem.backBarButtonItem = exampleButton;
-(void)backToViewController:(UIBarButtonItem*)sender
{
[self.navigationController popToRootViewControllerAnimated:YES];
}
IT will not add backBarButtonItem with your line as :
self.navigationItem.backBarButtonItem = exampleButton;
You have to Provide leftbarbutton as
UIBarButtonItem *exampleButton = [[UIBarButtonItem alloc] initWithTitle:#" " style:UIBarButtonItemStylePlain target:self action:(#selector(backToViewController:))];
// As you are going with " ", there is no text, that means you have to click at left side randomly, to get affect.
self.navigationItem.leftBarButtonItem = exampleButton;
Update 1
You can provide Custom button with Image created by you, as you want,
UIButton *barCutomBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[barCutomBtn setImage:[UIImage imageNamed:#"backImage.png"] forState:UIControlStateNormal];
[barCutomBtn addTarget:self action:#selector(backToViewController:)forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *exampleButton = [[UIBarButtonItem alloc] initWithCustomView:barCutomBtn];
self.navigationItem.leftBarButtonItem = exampleButton;
I'm trying to add button to navigation bar i want to add the button without any action any help please?
UIBarButtonItem *yourButton = [[UIBarButtonItem alloc]
initWithTitle:#"Your Button"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(methodName:)];
self.navigationItem.rightBarButtonItem = yourButton;
[yourButton release];
And then method
-(IBAction)methodName {
// your code here
}
You should add UIBarButtonItem to Nav bar instead of UIButton. T do that you can call this:
UIBarButtonItem *button = [[UIBarButtonItem alloc]
initWithTitle:#"Title"
style:UIBarButtonItemStyleBordered
target:nil
action:nil];
self.navigationItem.rightBarButtonItem = button;
UIBarButtonItem *customBtn=[[UIBarButtonItem alloc] initWithTitle:#"Custom" style:UIBarButtonItemStylePlain target:self action:#selector(customBtnPressed)];
[self.navigationItem setRightBarButtonItem:customBtn];
///// called event
-(IBAction)customBtnPressed:(id)sender
{
//Your code here
}
if UIButton is you really want, try this.
UIButton *btnSample = [[UIButton alloc] initWithFrame:btnFrame];
btnSample.backgroundColor = [UIColor blueColor];
UIBarButtonItem *barBtn_cart = [[UIBarButtonItem alloc] initWithCustomView:btnSample];
self.navigationItem.rightBarButtonItem = btnSample;
I had add image at my navigation bar and need to add the right bar refresh button.
But the right bar button can not show at the navigation after add.
Why?
Here the code add image at navigation bar:
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"topbar.png"] forBarMetrics:UIBarMetricsDefault];
Here the add right bar button at viewDidLoad:
UIBarButtonItem *refreshItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:#selector(refresh: )];
NSArray *actionButtonItems = #[refreshItem];
self.navigationController.navigationItem.rightBarButtonItems = actionButtonItems;
You should not add to self.navigationcontroller.
Do something like this will resolve your problem.
self.navigationItem.rightBarButtonItem = refreshItem;
Try with following code
UIBarButtonItem *refreshItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:#selector(refresh: )];
self.navigationItem.rightBarButtonItem = refreshItem;
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc]
initWithTitle:#"Refresh"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(rightButtoneClicked:)];
self.navigationItem.rightBarButtonItem = rightButton;
Right this code in viewDidLoad and you get the refresh button on right side.
Replace this
self.navigationController.navigationItem.rightBarButtonItems = actionButtonItems;
With:
self.navigationItem.rightBarButtonItems = actionButtonItems;
I have code like this in my navigation bar:
UIBarButtonItem *myButton = [[UIBarButtonItem alloc] initWithTitle:#"Next" style:UIBarButtonItemStylePlain target:self action:#selector(self)];
self.navigationItem.rightBarButtonItem = myButton;
What's the next step to grab when this is actually pressed?
You mad a mistake here by setting #selector(self).
You need to put here the actual function you need to fire:
UIBarButtonItem *myButton = [[UIBarButtonItem alloc] initWithTitle:#"Next" style:UIBarButtonItemStylePlain target:self action:#selector(doSomeThing)];
And of course:
- (void) doSomeThing{
}
Do like this
UIBarButtonItem *Button=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:#selector(myAction:)];
self.navigationItem.leftBarButtonItem=Button;
- (void)myAction:(id)sender
{
//do ur actions
}