setting action from navigation bar item created programmatically - ios

My storyboard is like this:
So from navigation controller , i am going to a tab controller.
My tab controller has its classed, named: MYTabBarView
In MyTabBarView.m , in its ViewDidLoad method I created the right bar button item programmatically like this:
UIBarButtonItem *homeButton = [[UIBarButtonItem alloc] initWithTitle:#"Home" style:UIBarButtonSystemItemAction target:self action:#selector(homeButtonAction)];
self.navigationItem.rightBarButtonItem=homeButton;
my question is how do I write an action for it. I mean if it was a button that was visible in the storyboard, I would "drag and dropped" it in the .h file and in the .m file I would have
the method:
Now what should I do?
- (IBAction)home:(id)sender {
....
}

Use the selector you typed when you created the button:
- (void)homeButtonAction {
// Code
}
And if you want to pass along the sender use #selector(homeButtonAction:) and the following method:
- (void)homeButtonAction:(id)sender {
// Code
}

Related

How to add a button to a UINavigation item?

I am converting an app that is written in ios 2 to ios 9. I am not really familiar with ios 2 and I need some help. Back in ios 2 there were no view controllers and xib files were used. There was one xib file called the main window which would hold all the navigation items. In my case the main window xib has a navigation bar. And in code there is the following line
self.navigationItem.title = #"Back";
So I could see when i run the app on a simulator, in one of my views there is a button with titled "Back". But i cannot find a method for this button. Like there is no IBAction for it or a method. I am trying to add a selector for the navigationitem but it doesn't work. This is what I tried:
self.navigationItem.selector = #selector(backButtonClicked:)
How can I add a selector to a navigationItem so I can search that in the project or create my own selector.
You can add custom back button to your navigation bar this way
UIBarButtonItem *btn = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStyleBordered target:self action:#selector(backButtonClicked:)];
self.navigationItem.leftBarButtonItem = btn;
-(void)backButtonClicked: (id)sender
{
[self.navigationController popViewControllerAnimated: YES]; // or popToRoot... if required.
}

how to make UIBarButtonItem action is to view the login page (main view controller)

i have 2 class the first is view controller and another one tableviewcontroller i insert UIBarbuttonItem to insert mew user i want the add button to target to the login page of view controller how can i make that
If I understood your question right, and you want to link the UIBarButtonItem to another ViewController - you can do so simply by ctrl + dragging from the UIBarButtonItem to another ViewController to create a segue between them.
Edit after comment:
Step-by-step:
First, ctrl + drag from your first ViewController to the second to create a segue connection between them
Then, use your code and create a method
Create a method to call the segue:
-(void)myMethod {
[self performSegueWithIdentifier:#"identifier" sender:self];
}
Specify this method like Victor Johnson shows and once myMethod is called, you will be taken to the other ViewController.
If all you're trying to do is switch from one view controller to another, you can ctrl + drag from the first view controller to create a segue between the two controllers, then give the segue a unique identifier (I'll use "SegueIdentifier"). Then in the first view controller create a new method that you can call each time the add button is pressed. Then you can set that as the selector for your add button.
- (void)goToSecondViewController {
[self performSegueWithIdentifier:#"SegueIdentifier" sender:self];
}
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(goToSecondViewController)];

present View Controller on click of Bar Button items

I have included numerous ViewControllers and as per the requirements, all the ViewControllers must contain the two same custom BarButton items(settings,help) in the right and so i created a separate class extending NSObject and included two UIButtons in it.
In all the ViewControllers, i imported this class and set barButtons like this:
NavigationBarButtonItems *nav=[[NavigationBarButtonItems alloc]init];
self.navigationItem.rightBarButtonItems = [nav BarButtonItems];
where, NavigationBarButtonItems is the class containing two custom Buttons in -(NSArray *)BarButtonItems; method.
Everything worked perfect. But Here is the problem, i created a viewController for one of the barButtons, say, 'settings' and i cant figure out how to present this ViewController(settings) when settings barButton is clicked as settings barButton is included in almost all the viewControllers.
Help me out. Thanks!
In the custom class Create the button like this.
-(void)createSettingbutton:(id)FromClass {
//Create Add category Button
UIButton *addSettingButton=[UIButton buttonWithType:UIButtonTypeCustom];
addSettingButton.frame=CGRectMake(0, 0, 30, 30);
[addSettingButton setBackgroundImage:[[UIImage alloc]initWithContentsOfFile:[[NSBundle mainBundle]pathForResource:#"" ofType:#"png"]] forState:UIControlStateNormal];
**[addSettingButton addTarget:FromClass action:#selector(ButtonAction) forControlEvents:UIControlEventTouchUpInside];**
UIBarButtonItem *_addSettingButton=[[UIBarButtonItem alloc]initWithCustomView:addSettingButton];
[self.navigationItem setRightBarButtonItem:_addSettingButton];
[_addSettingButton release];
_addSettingButton=nil;
}
Now add the following code in the viewcontroller where you want to add the setting button.
[self createSettingbutton:self];//Second self is the parameter which is passed to 'FromClass' Variable.
Now in the same viewcontroller add the action for setting button
-(void)ButtonAction
{
[self.view addSubview:mSettingView.view];
//or
[self.navigationController pushViewController:mSettingView animated:YES]
}

have a universal bar button item in navigationController

I Have UINavigationController in my app, I want to have a right UIBarButtonItem to be shown in all navigation bar that appear in my application. this button will load menu, so I don't want to add this button in every navigation bar manually, also as the function is loading menu, I don't want to copy/past action for this button.
is there any way to handle this in ViewController.h and .m ?
so the button act as a universal bar button item?
What you can do is subclass the navigation controller. Here is an example
#interface NavigationController : UINavigationController
#end
#interface NavigationController () <UINavigationBarDelegate>
#end
#implementation NavigationController
- (void)viewDidLoad
{
[super viewDidLoad];
for (UIViewController* viewController in self.viewControllers){
// You need to do this because the push is not called if you created this controller as part of the storyboard
[self addButton:viewController.navigationItem];
}
}
-(void) pushViewController:(UIViewController *)viewController animated:(BOOL)animated{
[self addButton:viewController.navigationItem];
[super pushViewController:viewController animated:animated];
}
-(void) addButton:(UINavigationItem *)item{
if (item.rightBarButtonItem == nil){
item.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:#selector(action:)];
}
}
-(void) action:(UIBarButtonItem*) button{
}
#end
This cannot be done unless you use a custom view to look like NavigationBar.
By default, NavigationController clears all bar button items when a ViewController is pushed or popped. So for every ViewController, you need to create UIBarButtonItem every time in function
- (void)viewWillAppear:(BOOL)animated
or use can subclass UINavigationController and do as #rp90 answer.
You can add a button that appears in your UINavigationController by adding it in the UINavigationController's delegate - in this example, a singleton helper class.
Swift 3:
class NavHelper: UINavigationControllerDelegate {
static let shared = NavHelper()
func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {
let barButtonItem = UIBarButtonItem(image: <yourButtonImage>, style: .plain, target: NavHelper.shared, action: #selector(NavDelegate.handleButton(_:)))
viewController.navigationItem.rightBarButtonItem = barButtonItem
}
func handleButton(_ sender: UIBarButtomItem) {
<yourCode>
}
}
Another option is to make your own navigation bar view as a part of the UIViewController. You can turn off Apple's and build your own. We did this to provide our own controls easier. The only thing you lose is the easy translucency under iOS 7. A lot of apps do this for the same reason we did.
You can create a new class that inherits from UIViewController and in the viewDidLoad method, create a UIBarButtonItem and add it to the navigationItem as a left/right bar item. Let's say this class is called CustomBarViewController:
UIBarButtonItem *barItem = [[UIBarButtonItem alloc] initWithTitle:#"hi" style:UIBarButtonItemStylePlain target:self action:#selector(doSomething:)];
[self.navigationItem setRightBarButtonItem:barItem];
In your existing view controllers, instead of having them inherit from UIViewController in the .h file, you can have them use CustomBarViewController instead:
#interface MyExistingViewController : CustomBarViewController
You can then put actions into the doSomething: method, or have it pass notifications to your existing view controllers.
You can add the button into a ContainerView. Thus it will be on at all times.
Food for thought....

How can i assign an action to rightbarbuttonitem with an edit style?

I have an edit rightbarbuttonitem in my view in navigation bar. I set it up with the help of storyboard/IB, not programmatically. Now, all i want is to assign an action when the "done" barbuttonitem is pressed (not edit).
Is there a way to achieve it? I tried manually through -(IBAction), but it's not working. Also, i want to perform the action on selected items in UITableView. So if you give me an idea, it would be great.
That button calls the method
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
You can implement it and it will get called everytime your edit/done button gets tapped. All you have to do is check the button's title property to see when it's showing done and when it's showing edit
If you declared your button as an IBOutlet then all you'd need to do is use the synthesised variable on your .m as so:
_yourBarButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(runMethod)];
self.navigationItem.rightBarButtonItem = _yourBarButton;
Then you'd have to declare your run method:
-(void)runMethod
{
//do stuff
}

Resources