Disabling RightBarButtonItems - ios

My viewController is functioning as a container and has its own UINavigationBar. It is not in a naviagtion controller. My nav bar items are set up like so...
self.navigationItem.leftBarButtonItems = leftItems;
self.navigationItem.rightBarButtonItems = #[logout, settings];
[self.navBar setItems:#[self.navigationItem]];
At various points in the application this navigation bar is locked down until the user completes a task. This snippet works fine for toggling the enabled property of the buttons in the navigation bar but only on the leftBarButtonItems! Why?
for(UIBarButtonItem *rightButton in self.navigationItem.rightBarButtonItems){
[rightButton setEnabled:!rightButton.enabled];
}
for(UIBarButtonItem *leftButton in self.navigationItem.leftBarButtonItems){
[leftButton setEnabled:!leftButton.enabled];
}

UPDATE:
I had created a test demo, it works well. Here is the sreenshots and code, hope to give you some help!
ViewController.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
#property(nonatomic,strong) UINavigationItem * navItem;
#property(nonatomic,assign) IBOutlet UINavigationBar * navBar;
#end
ViewController.m
#import "ViewController.h"
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
UIBarButtonItem* barItem1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(barItemClicked:)];
UIBarButtonItem* barItem2 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(barItemClicked:)];
UIBarButtonItem* barItem3 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(barItemClicked:)];
UIBarButtonItem* barItem4 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(barItemClicked:)];
self.navItem = [[UINavigationItem alloc] init];
self.navItem.leftBarButtonItems = #[barItem1,barItem2];
self.navItem.rightBarButtonItems = #[barItem3,barItem4];
[self.navBar setItems:#[self.navItem]];
}
- (IBAction)anableSwitch:(id)sender{
UISegmentedControl * swith = (UISegmentedControl *)sender;
for(UIBarButtonItem *rightButton in self.navItem.leftBarButtonItems){
[rightButton setEnabled:(swith.selectedSegmentIndex == 0)];
}
for(UIBarButtonItem *leftButton in self.navItem.rightBarButtonItems){
[leftButton setEnabled:(swith.selectedSegmentIndex == 0)];
}
}
- (void)barItemClicked:(id)sender{
NSLog(#"barItemClicked");
}

Related

How to show fullscreen popovercontroller in iPhone screen?

I have UIbarbuttonItem in navationbar and than click the button works well iPad simulator but don't work iPhone simulator because Popover not supported iPhone. I trying to show fullscreen popovercontroller in iPhone screen.How to fix this problem. I add my codes and screenshots.
Thanks in advance.
MyCodes;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIBarButtonItem *shareItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:nil];
UIBarButtonItem *cameraItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:#selector(doneButtonPressed:)];
UIBarButtonItem *searchItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:nil];
UIBarButtonItem *flexibleItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
NSArray *actionButtonItems = #[shareItem, cameraItem, flexibleItem,searchItem];
self.navigationItem.rightBarButtonItems = actionButtonItems;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)doneButtonPressed:(id)sender
{
if(![popoverController isPopoverVisible]){
UIViewController *viewControllerForPopover = [self.storyboard instantiateViewControllerWithIdentifier:#"MyIdentifier"];
popoverController = [[UIPopoverController alloc] initWithContentViewController:viewControllerForPopover];
popoverController.delegate=self;
[popoverController presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
//popoverController.passthroughViews=[NSArray arrayWithObjects:self.view, nil];
}
else{
[popoverController dismissPopoverAnimated:YES];
}
}
#interface ViewController : UIViewController<UIPopoverControllerDelegate,UIPopoverPresentationControllerDelegate>{
UIPopoverController *popoverController;
CameraViewController *cameraPopOver;
}
My Screens;
PopoverController can't be added to iPhone as this controller is only for iPad and not for iOS. to solve your issue you can take a simple view or a tableview over there, which will appear on click event of bar button
I once used KGModal.
KGModal is an easy drop in control that allows you to display any view in a modal popup. Can popup UIView and UIViewController.

Right Button won't show in custom Navigation Controller

My app is a tab bar app with many navigation controllers.
I want these navigation controllers to handle login/logout actions with a custom right bar button. So I've set up my tab bar in AppDelegate this way :
MyFirstViewController *firstViewController = [[MyFirstViewController alloc] init];
UIViewController *firstNavigationController = [[CustomNavigationController alloc]
initWithRootViewController:firstViewController];
MySecondViewController *secondViewController = [[MySecondViewController alloc] init];
UIViewController *secondNavigationController = [[CustomNavigationController alloc]
secondViewController]
initWithRootViewController:secondViewController];
....
[tabBarController setViewControllers:#[firstNavigationController, secondNavigationController]];
Then CustomNavigationController :
#implementation ConnectionNavigationController
- (void) viewDidLoad
{
[self displayConnectionButton];
}
- (void) displayConnectionButton
{
UIImage *portraitImage, *landscapeImage;
portraitImage = [UIImage imageNamed:#"conn.png"];
landscapeImage = [UIImage imageNamed:#"connLandscape.png"];
UIBarButtonItem *connButton = [[UIBarButtonItem alloc]
initWithImage:[portraitImage
imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]
landscapeImagePhone:[landscapeImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]
style:UIBarButtonItemStylePlain target:self action:#selector(showConnectionPopup)];
self.navigationItem.rightBarButtonItem = connButton;
}
#end
I tried with viewDidLoad, viewDidAppear, viewWillAppear but nothing works, I can't get this button to show in the navigation bar. I also tried to add reloadInputViews or setNeedsDisplay. What should I do?
Thanks for your help
Edit : interface of CustomNavigationController
#interface ConnectionNavigationController : UINavigationController
- (void) displayConnectionButton;
#end
From what I understand you should add the displayConnectionButton method to the relevant UIViewController subclass and not to your UINavigationController subclass as this only contains the stack of view controllers.
E.g.
#implementation MyFirstViewController
- (void) viewDidLoad
{
[self displayConnectionButton];
}
- (void) displayConnectionButton
{
UIImage *portraitImage, *landscapeImage;
portraitImage = [UIImage imageNamed:#"conn.png"];
landscapeImage = [UIImage imageNamed:#"connLandscape.png"];
UIBarButtonItem *connButton = [[UIBarButtonItem alloc]
initWithImage:[portraitImage
imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]
landscapeImagePhone:[landscapeImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]
style:UIBarButtonItemStylePlain target:self action:#selector(showConnectionPopup)];
self.navigationItem.rightBarButtonItem = connButton;
}
#end
As a note if you put a breakpoint on your displayConnectionButton in your CustomNavigationController and po self.viewControllers I think you will get an empty array (i.e. no view controllers)

UINavigationBar UIBarButtonItem works on left but not on right

I have two buttons on my navbar. One on the left and one on the right. The button on the left works but the one on the right does not work. If I swap the buttons, recompile and load the issue persist with the right button regardless of which button is in the right spot of the bar.
Also while the left button works as soon as I try to use the right button the left button stops working too.
How do I make the right button work? Below is the code.
UINavigationBar *NavBar = [[UINavigationBar alloc] init];
[NavBar sizeToFit];
[self.view addSubview:NavBar];
UIBarButtonItem *cmdBack = [[UIBarButtonItem alloc] initWithTitle:#"Back"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(cmdBackClicked:)];
UIBarButtonItem *cmdAddDevice = [[UIBarButtonItem alloc] initWithTitle:#"Add Device"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(cmdAddDeviceClicked:)];
UINavigationItem *NavBarItems = [[UINavigationItem alloc] initWithTitle:#"Settings"];
NavBarItems.leftBarButtonItem = cmdBack;
NavBarItems.rightBarButtonItem = cmdAddDevice;
NavBar.items = [NSArray arrayWithObjects:NavBarItems, nil];
The functions for the buttons are these
- (void) cmdBackClicked:(id)sender
{
[self.view removeFromSuperview];
}
- (void) cmdAddDeviceClicked:(id)sender
{
vcAddDevice *ViewAddDevice = [[vcAddDevice alloc] initWithNibName:#"vcAddDevice" bundle:nil];
[ViewAddDevice SetEditDeviceMode:[NSString stringWithFormat:#"No"]];
[self.view addSubview:ViewAddDevice.view];
}
In the .h file I have this.
#import <UIKit/UIKit.h>
#import "vcAddDevice.h"
#interface ViewControllerSettings : UIViewController<vcAddDeviceControllerDelegate, UITableViewDelegate, UITableViewDataSource>
- (IBAction) cmdBackClicked:(id) sender;
- (IBAction) cmdAddDeviceClicked:(id) sender;
#end

Push a UIBarButtonSystemItemAdd to a different view controller

I'm trying to push an add button to a different view controller and I'm not using a storyboard. The button appears but doesn't do anything when clicked. Here's the code I have:
-(void)pushLogin
{
[self.navigationController pushViewController:[[AddListingViewController alloc] init] animated:YES];
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationItem.title = #"Listings";
UIBarButtonItem *addButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:nil
action:#selector(pushLogin)];
self.navigationItem.rightBarButtonItem = addButton;
}
Any suggestions?
Target should be the viewcontroller, not nil
UIBarButtonItem *addButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:self
action:#selector(pushLogin)];

How to add a right bar button to a navigation bar in iphone

I want to add a right bar button item to the navigation bar, so that on click, it performs certain a function.
I have created the following code to add the right bar button item, but after it is done, the bar button item is not getting displayed in navigation bar:
-(void)viewDidload{
self.navigationItem.rightBarButtonItem =
[[[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:self
action:#selector(Add:)] autorelease];
}
-(IBAction)Add:(id)sender
{
TAddNewJourney *j=[[TAddNewJourney alloc]init];
[app.navigationController pushViewController:j animated:YES];
[j release];
}
Let assume, you have a UIViewController, like this:
UIViewController *vc = [UIViewController new];
And you added it to navigation controller:
UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:vc];
In this way rightBarButtonItem will NOT getting displayed:
nc.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"xyz" style:UIBarButtonItemStyleDone target:self action:#selector(xyz)];
BUT this way IT WILL appear:
vc.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"xyz" style:UIBarButtonItemStyleDone target:self action:#selector(xyz)];
Use your own viewcontroller instead of the navigationcontroller to refer navigationItem.
-(void)viewDidLoad
{
[super viewDidLoad];
app.navigationController.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(Add:)] autorelease];
}
-(IBAction)Add:(id)sender
{
TAddNewJourney *j=[[TAddNewJourney alloc]init];
[app.navigationController pushViewController:j animated:YES];
[j release];
}
Try the other answers. I posted this answer so that it will work if your viewcontroller does not have a navigation controller which i think is the problem.
Add this code in viewdidload
UIBarButtonItem *chkmanuaaly = [[UIBarButtonItem alloc]initWithTitle:#"Calculate" style:UIBarButtonItemStylePlain target:self action:#selector(nextview:)];
self.navigationItem.rightBarButtonItem = chkmanuaaly;
Most of the answers here address setting the UINavigationBar's rightBarButtonItem from within the new VC after it is shown. My need, which was actually answered by János, is to set the UINavigationItem items from the CALLING UIViewController. Hence, the following code (Thanks, János).
// from within self (aka the calling controller):
UIViewController *c = [[[UIViewController alloc] init...] autorelease];
c.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(dismissModalViewControllerAnimated:)] autorelease];
c.navigationItem.title = #"Title Goes Here";
UINavigationController *nav = [[[UINavigationController alloc] initWithRootViewController:c] autorelease];
nav.navigationBarHidden = FALSE;
[self presentModalViewController:nav animated:YES];
Now, granted, this may seem redundant to János' answer, but I need more rep points in order o mark his response up. ;-)
UIBarButtonItem *add=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(addUser)];
self.navigationItem.rightBarButtonItem=add;
[add release];
I guess it will be more complete response and it should help in any situation:
-(void)viewDidLoad{
//Set Navigation Bar
UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 64)];
//Set title if needed
UINavigationItem * navTitle = [[UINavigationItem alloc] init];
navTitle.title = #"Title";
//Here you create info button and customize it
UIButton * tempButton = [UIButton buttonWithType:UIButtonTypeInfoLight];
//Add selector to info button
[tempButton addTarget:self action:#selector(infoButtonClicked) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem * infoButton = [[UIBarButtonItem alloc] initWithCustomView:tempButton];
//In this case your button will be on the right side
navTitle.rightBarButtonItem = infoButton;
//Add NavigationBar on main view
navBar.items = #[navTitle];
[self.view addSubview:navBar];
}
Use the following code:
UIBarButtonItem *add=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(addUser)];
self.navigationItem.rightBarButtonItem=add;
[add release];
Hope this helps you out.
Enjoy!
Swift:
let rightButton = UIBarButtonItem(image: UIImage(named: "imageName"),
style: .plain,
target: self,
action: #selector(buttonTapped))
navigationItem.setRightBarButton(rightButton, animated: false)
#objc func buttonTapped(sender: UIBarButtonItem!) {
// Implement action
}
UIButton *dButton=[UIButton buttonWithType:0];
dButton.frame=CGRectMake(50,50,50,50);
[dButton addTarget:self action:#selector(clickdButton:)
forControlEvents:UIControlEventTouchUpInside];
[dButton setImage:[UIImage imageNamed:#"iconnavbar.png"]
forState:UIControlStateNormal];
dButton.adjustsImageWhenHighlighted=NO;
dButton.adjustsImageWhenDisabled=NO;
dButton.tag=0;
dButton.backgroundColor=[UIColor clearColor];
UIBarButtonItem *RightButton=[[[UIBarButtonItem alloc] initWithCustomView:dButton]autorelease];
self.navigationItem.rightBarButtonItem=RightButton;
and then:
-(IBAction)clickdButton:(id)sender{
classexample *tempController=[[classexample alloc] init];
[self.navigationController pushViewController:tempController animated:YES];
[tempController autorelease];
}

Resources