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.
Related
I have a tab-based application. In SecondViewController, there is a UITableView with several cells. When you swipe on a cell, you are sent to another view controller, speciesController. The below code works to send the app to another view, and it does set up a navigation bar, but there is no back button.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
SpeciesViewController* speciesController = [[SpeciesViewController alloc]initWithNibName:#"SpeciesViewController" bundle:nil];
Species *theSpecies = [fetchedResultsController objectAtIndexPath:indexPath];
speciesController.theSpecies = theSpecies;
switch (sortBySegmentedControl.selectedSegmentIndex) {
case kSortByCommonNameFirst:
speciesController.title = [theSpecies commonNameFirstLast];
break;
case kSortByCommonNameLast:
speciesController.title = [theSpecies commonNameLastFirst];
break;
case kSortByScientificName:
speciesController.title = [[NSString alloc] initWithFormat:#"%#%#",
[theSpecies.scientificName substringToIndex:1],
[[theSpecies.scientificName substringFromIndex:1] lowercaseString]];
break;
default:
break;
}
speciesController.hidesBottomBarWhenPushed = YES;
//self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStylePlain target:nil action:nil];
//// Store current index path for viewDidAppear animation. ////
self->currentSelectedIndexPath = indexPath;
UINavigationController *navBar=[[UINavigationController alloc]initWithRootViewController:speciesController];
navBar.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStylePlain target:nil action:nil];
[self presentViewController:navBar animated:YES completion:nil];
}
I realize that to have a backbutton, you need to push the current view onto the navigation controller stack. However, changing
navBar.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStylePlain target:nil action:nil];
[self presentViewController:navBar animated:YES completion:nil];
to
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStylePlain target:nil action:nil];
[self.navigationController pushViewController:navBar animated:NO];
Doesn't work. In fact, the app fails to change to a different view, and I believe this is because there is no self.navigationController
I tried to add/initialize one with this line:
UINavigationController *navigationController=[[UINavigationController alloc]initWithRootViewController:self];
but that caused the application to crash.
I appreciate any information about adding a navigation controller to an app (in storyboard or programatically). I'm afraid most of the examples I saw were outdated, from 2011 or so with methods like self.window addSubview...
Thank you!
Instead of having SecondViewController as a Tab Item, set that Tab Item to a Navigation Controller with SecondViewController as its Root VC.
Then, on didSelectRowAtIndexPath, all you have to do is:
[self.navigationController pushViewController: speciesController animated:YES];
The "back button" will automatically be there.
after
UINavigationController *navBar=[[UINavigationController alloc]initWithRootViewController:speciesController];
try
speciesController.navigationController.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStylePlain target:nil action:nil];
if (self.navigationController)
[self.navigationController pushViewController:navBar animated:NO];
else
[self presentViewController: navBar animated:YES completion:nil];
I am using the regular QuickDialog controller code from their tutorial:
QRootElement *root = [[QRootElement alloc] init];
root.title = #"Hello"
root.grouped = YES;
QSection *section = [[QSection alloc] init];
QEntryElement *hello = [[QEntryElement alloc] initWithTitle:#"Hello World" Value:#""];
[root addSection:section];
[info addElement:hello];
UINavigationController *navigation = [QuickDialogController controllerWithNavigationForRoot:root];
[self presentModalViewController:navigation animated:YES];
How can I add a 'cancel' button to the navigation bar? I tried:
navigation.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel handler:^(id sender){
[self.navigationController.modalViewController dismissModalViewControllerAnimated:YES];
}];
... but that didn't work. Any suggestions?
The proper way to do this is to adjust the code for the `leftBarButtonItem' like this:
navigation.navigationBar.topItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel handler:^(id sender){
[self.navigationController.modalViewController dismissModalViewControllerAnimated:YES];
}];
I know this is late but hopefully this will help someone else. I had a similar problem, and was able to use the following code to create a button to remove the form from view
navigation.navigationBar.topItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Close" style:UIBarButtonItemStyleDone target:self action:#selector(dismissModalViewControllerAnimated:)];
I tried your code in my sample logic and achieved the cancel button in the following way.
You need to set your navigationItem's leftbarbuttonitem on the viewcontroller of a navigation controller, instead of setting straight to your navigationcontroller.
// -- present your root controller.
UINavigationController *navigation = [QuickDialogController controllerWithNavigationForRoot:root];
[self presentModalViewController:navigation animated:YES];
[[[navigation topViewController] navigationItem] setLeftBarButtonItem:[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel handler:^(id sender){
[self.navigationController dismissModalViewControllerAnimated:YES];
}]];
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)];
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.
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];
}