Push a UIBarButtonSystemItemAdd to a different view controller - ios

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

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.

Done button item has a text of Edit

I have a navbar with a rightbarbuttonitem of:
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonItemStyleDone target:self action:#selector(done)];
Though when I compile the app onto a device, the button says edit on it instead of done like it is supposed to. Why is this
I add the button like this:
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonItemStyleDone target:self action:#selector(done)];
This is the whole code for the view:
-(void) done {
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = #"Edit";
self.view.backgroundColor = [UIColor whiteColor];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonItemStyleDone target:self action:#selector(done)];
// Do any additional setup after loading the view.
}
I am not using an xib file just pure code and am presenting to the controller from another
Your issue is that you are using the style of a done button which means it will say edit instead of done as it is basically an edit button which doesn't respond to some selectors edit buttons do. To fix this change your code to this:
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonItemStyleDone target:self action:#selector(done)];

Can't show popover controller with UIBarButtonItem

i use storyboard for developing UI for my project. There are many issues were solved, but this problem killing me. I have added action for UIBarButtonItem :
- (IBAction)pressAddActionButton:(UIBarButtonItem *)sender {
if (_mode == itemSelect) {
LookUpTableViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:#"lookupTable"];
vc.key = #"title";
vc.data = [Linesheet MR_findAllSortedBy:#"title" ascending:YES];
vc.lookUpDelegate = self;
self.myPopoverController = [[UIPopoverController alloc] initWithContentViewController:vc];
[self.myPopoverController presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
} else {
self.mode = itemSelect;
}
}
If i using storyBoard segue for showing popover - all good, but if i do it in runtime a popover does not show. I should manually create UIBarButtonItem.
Thanks for help!!!
Update, code for buttons:
- (void)setupNavigationItems {
self.navigationController.navigationBarHidden = NO;
UIBarButtonItem *addItem;
if (_mode == itemSelect) {
addItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:self
action:#selector(pressAddActionButton:)];
} else {
addItem = [[UIBarButtonItem alloc] initWithTitle:#"Done" style:UIBarButtonItemStyleDone
target:self
action:#selector(pressDoneButton:)];
}
[addItem setStyle:UIBarButtonItemStyleBordered];
UIBarButtonItem *separator = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:self
action:nil];
UIBarButtonItem *action = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction
target:self
action:#selector(pressActionButton:)];
[action setStyle:UIBarButtonItemStyleBordered];
[toolbar setItems:[NSArray arrayWithObjects:separator, addItem, action, nil] animated:YES];
}
Make sure you're hitting the code as you expect using a breakpoint on the presentPopoverFromBarButtonItem line. Also, make sure that the myPopoverController property is declared strong if you're using arc, as otherwise it will be nil before the presenting line.
Did you check if LookUpTableViewController *vc actually gets a proper instance? If not then check that your storyboard does have #"lookupTable" set as your controller's identifier.
The code is right. Try replacing the sender with self.navigationItem.leftBarButtonItem or self.navigationItem.rightBarButtonItem. The sender may not be what you are expecting.
Also remove the if (_mode == itemSelect) clause for testing I'm not sure why you need to access the ivar _mode ivar directly.

UIBarButtonItem - How do I make a "done" button pop?

I got a navigation controller that has a "Done" button. When the user is finished with the form, they press "Done" and I want that view popped off the stack and back at the main menu.
Here is the code I got so far:
UIBarButtonItem *anotherButton = [[UIBarButtonItem alloc] initWithTitle:#"Done"
style:UIBarButtonItemStylePlain
target:self
action:[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:1] animated:YES]];
self.navigationItem.rightBarButtonItem = anotherButton;
Please help! Thank you
How about you try this:
UIBarButtonItem *anotherButton = [[UIBarButtonItem alloc] initWithTitle:#"Done"
style:UIBarButtonItemStylePlain
target:self
action:#selector(onClickOfDone);
self.navigationItem.rightBarButtonItem = anotherButton;
Now write the popViewController logic in the method named onClickOfDone
- (void)onClickOfDone {
[self.navigationController popViewControllerAnimated:YES];
}
If your pop is only one level back then the above code would help. If you want to specifiy the controller to which it has to pop to, then you can use
[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:1] animated:YES]
EDIT:
You could also use the SystemItem for Done:
UIBarButtonItem *aDoneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
target:self action:#selector(onClickOfDone)];
self.navigationItem.rightBarButtonItem = aDoneButton;
[aDoneButton release];
1) Define new method of the class that corresponds to self. Fro example,
- (void)closeView
{
[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:1] animated:YES];
}
2) Set appropriate selector when creating your button:
UIBarButtonItem *anotherButton = [[UIBarButtonItem alloc] initWithTitle:#"Done"
style:UIBarButtonItemStylePlain
target:self
action:#selector(closeView)];
self.navigationItem.rightBarButtonItem = anotherButton;
[anotherButton release]; // and don't forget to clear memory
Your code is correct just make one function and inside that function write popping code, then call that function using #selector in action.

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