I need to create the UIToolbar and the UIBarButtonItem programmatically. I'm using Storyboard for the layout.
How is now a new view opened by pressing the toolbar button which was programmatically created? (I know how to do it with Segue when the button is created in the Interface Builder but I can't use this approach here).
First of all, create the UIBarButtonItem using the following code :
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonItemStylePlain target: self action: #selector(showNewView)];
Then,
-(void)showNewView
{
UIStoryboard *story =[UIStoryboard storyboardWithName:#"MainStoryboard_iPhone" bundle:nil];
UIViewController *newViewController = [[postDetailViewControllerIphone alloc]init];
newViewController=[story instantiateViewControllerWithIdentifier:#"newView"];
[self presentViewController:newViewController animated:YES completion:nil];
}
This code will allow the barButtonItem to open the new view controller.
If u haven't added barButtonItem to the toolbar then do this after creating barButtonItem.
NSArray *toolBarItems = [[NSArray alloc] initWithObjects:barButtonItem,nil];
[self.toolBar setItems:bottomtoolBarItems];
This will add your barButtonItem to the toolbar.
When you create your UIBarButtonItem make sure you set the target and action. Then in the called method, present your next view controller.
Create the bar button item.
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonItemStylePlain target: self action: #selector(presentVC:)];
Present the view controller.
-(void)presentVC:(id)sender
{
[self presentViewController: otherViewController animated: YES completion:^{
// COMPLETION BLOCK
}];
}
Related
Have tried all the examples on this website I just don't see anything on my modal view, I do see the navigationbar though but its empty
EditEntityViewController *editEntityViewController = [[EditEntityViewController alloc] init];
editEntityViewController.currentNode = newNode;
editEntityViewController.delegate = self;
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:editEntityViewController];
navController.modalPresentationStyle = UIModalPresentationFormSheet;
navController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
UIBarButtonItem *anotherButton = [[UIBarButtonItem alloc] initWithTitle:#"Show"
style:UIBarButtonItemStylePlain
target:self
action:#selector(refreshPropertyList:)];
editEntityViewController.navigationItem.rightBarButtonItem = anotherButton;
[anotherButton release];
[self presentModalViewController:navController animated:YES];
[editEntityViewController release];
As discussed, your code was correct and is the standard way to show a popup sheet with a UINavigationBar to hold buttons to dismiss the sheet. However, you had defined an IBOutlet in EditViewController called navigationItem, which was causing a conflict.
Try setting the rightBarButtonItem on editEntityViewController before you create the UINavigationController with initWithRootViewController:.
I think that the navigation bar is set up when the UINavigationController is created. Adding the right bar item after creation time is too late.
EDIT: Ok, so that's not the issue.
The following minimal code snippet works so I would check whether your EditEntityViewController is doing something to remove the button elsewhere:
- (IBAction)showPopup:(id)sender
{
UIViewController *popupController = [[UIViewController alloc] init];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:popupController];
navController.modalPresentationStyle = UIModalPresentationFormSheet;
navController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
UIBarButtonItem *anotherButton = [[UIBarButtonItem alloc] initWithTitle:#"Show"
style:UIBarButtonItemStylePlain
target:self
action:nil];
popupController.navigationItem.rightBarButtonItem = anotherButton;
[anotherButton release];
[self presentModalViewController:navController animated:YES];
[popupController release];
}
The reason why this was not working is really stupid. Basically I had an IBOutlet defined in EditViewController called navigationItem which was conflicting with the SDK's property with the same name.
I removed it and the link from the nib and as Robin says it works perfectly.
Modally presented view controllers on navigation controllers don't have navigationItem nor navigationController properties. They DO, however, have parentViewController property, but this is irrelevant in your case.
If you want to customize navigation bar on your modally presented view, you should connect IBOutlet from view controller managing that view to the navigation bar placed in that managed view. Then do the manipulation through IBOutlet instance variable.
I want to build Grouped View in Modal View which is destination state from "+" button on the Navigation bar in main table view.
I have written this code:
-(void)viewDidLoad
{
//title
self.title = #"Set";
//addBtn
UIBarButtonItem *addBtn = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(toggleEditing)];
self.navigationItem.rightBarButtonItem = addBtn;
[addBtn release];
}
-(IBAction)toggleEditing
{
}
create a subclass of UITableViewController named MyTableViewController
use presentModalViewController:animated: in your current viewController
-(IBAction)toggleEditing {
MyTableViewController *tableViewController = [[[MyTableViewController alloc] initWithStyle:UITableViewStyleGrouped] autorelease];
[self presentModalViewController:tableViewController animated:YES];
}
And you will need a delegate method that tells the viewController from where you showed the modal viewcontroller that a row was selected.
How can a button be added to a navigation toolbar in an application which is navigation based?
The rootController is a tableView by default, and this is perfect. I would like to add a "share" button to the navigation controller so that I can attach it to my own method.
How is this done if the navigation bar is added in the code somewhere?
To do it in code, go to your views viewDidLoad method and create a UIBarButtonItem
This code will create a button that says share like you wanted and put it on the right hand side of the navigation bar.
- (void)viewDidLoad {
[super viewDidLoad];
UIBarButtonItem *shareButton = [[UIBarButtonItem alloc] initWithTitle:#"Share" style:UIBarButtonItemStyleBordered target:self action:#selector(share)];
self.navigationItem.rightBarButtonItem = shareButton;
[shareButton release];
}
UIBarButtonItem *shareButton = [[[UIBarButtonItem alloc] initWithTitle:#"Share" style:UIBarButtonItemStylePlain target:self action:#selector(yourShareAction)] autorelease];
self.navigationItem.rightBarButtonItem = shareButton;
This makes an add button, but you get the idea.
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:
self action:#selector(buttonPressed:)]
autorelease];
You can create an UIBarButtonItem and attach it to a root view controller's navigation item. You add a target-action pair when you initialize the UIBarButtonItem object and that action message will be sent to the target on a user tapping on the button.
So if you want the button when the table view is shown, set the UIBarButtonItem to your table view controller's navigationItem.rightBarButtomItem property.
I have created a UITabBar and UITabBarItems without UITabBarController on it, now i want to know how to place an action on click of the UITabBarItem.What is the method i should use for action on UITabBarItem?
You can't set an action on a UITabBarItem object directly. Instead, your view controller should implement the following UITabBarDelegate method:
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item;
This method is called when the user selects a tab (i.e. UITabBarItem).
Are you using a UINavigationController? If so, from the active view controller subclass you get the navigationItem and add the buttons to it, e.g.:
- (void) viewWillAppear:(BOOL)animated;
{
[super viewWillAppear: animated];
UIBarButtonItem * leftButtonItem = [[[UIBarButtonItem alloc] initWithTitle: #"Don't Show Again" style: UIBarButtonItemStyleBordered target: self action: #selector(permanentlyCloseWelcomeView)] autorelease];
[[self navigationItem] setLeftBarButtonItem: leftButtonItem];
}
Can you get away with using instances of UIToolbar and UIBarButtonItem instead? It could be more straightforward.
toolBar = [[UIToolbar alloc] init];
newPlayerItem = [[UIBarButtonItem alloc] initWithTitle:#"+"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(newPlayer:)];
NSArray *toolBarItemsArray = [[NSArray alloc] initWithObjects:newPlayerItem, nil];
[toolBar setItems:toolBarItemsArray];
[toolBarItemsArray release];
There is a better method than didSelectItem:
for each TabBarItem you create an action:
[item1 setAction:#selector(pressItem1:)];
[item2 setAction:#selector(pressItem2:)];
[item3 setAction:#selector(pressItem3:)];
[item4 setAction:#selector(pressItem4:)];
and then you can use the new actions:
-(void)pressItem1:(UITabBarItem *) item1 {<br/>
// Here comes your code which<br/>
// occurs after pressing item1.<br/>
}
That works for me
I have an UITableViewController which I would like to add UIToolbar to with one button. In the
- (void)viewDidLoad;
method of UITableViewController I have:
- (void)viewDidLoad {
[super viewDidLoad];
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:self
action:#selector(pressButton1:)];
self.navigationItem.title = #"Some title";
self.navigationItem.leftBarButtonItem = button;
}
Unfortunately I don't see the toolbar when I run my app.
Any hints? Should I do something more?
The navigationItem property of a view controller is useless if that controller is not displayed inside a UINavigationController.
If your view controller is inside a navigation controller I don't know what the problem is.
Otherwise you can use an UINavigationItem but you need to create a UINavigationBar yourself.
Either in the Interface Builder (add a UINavigationBar and add a UINavigationItem, then connect the UINavigationItem to a property outlet declared your view controller (you don't need to connect the Bar).
Or in your code :
UIBarButtonItem *item = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:self action:#selector(pressButton1:)];
UINavigationItem* navItem = [[UINavigationItem alloc] init];
navItem.rightBarButtonItem = item;
navItem.title = #"Your title";
naviBar = [[UINavigationBar alloc] init];
naviBar.items = [NSArray arrayWithObject:navItem];
naviBar.frame = CGRectMake(0.0, 0.0, self.view.frame.size.width, 44.0);
[self.view addSubview:naviBar];
[navItem release];
Your method requires an autorelease:
- (void)viewDidLoad {
[super viewDidLoad];
UIBarButtonItem *button = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(pressButton1:)] autorelease];
self.navigationItem.title = #"Some title";
self.navigationItem.leftBarButtonItem = button;
}
There's nothing wrong with your code per se. Your question states you want to add an UIToolBar to your view? Really? Or do you just want to add a button to the NavigationItem for UITableView?
If you don't have to use a UITableViewController and are not using a UINavigationController in your app already, you can set your view controller up as a regular UIViewController with a toolbar and tableview.
To do this in IB, drag out a UIViewController object and add a toolbar and tableview. Hook up outlets for both and set the delegate and datasource of the tableview to Files Owner. Add any other toolbar items or buttons and give them outlets and methods if you need them for buttons, etc. In your ViewController.h file, make sure you sign it up to conform to the UITableViewDataSource and UITabBarDelegate:
#interface ViewController : UIViewController <UITableViewDataSource, UITabBarDelegate>
From there, just build out your tableview delegate and datasource methods like you normally would, and write your button action methods for any buttons you added to the toolbar.
You just didn't show the toolbar. It is hidden by default. To fix it, you just put this line of code:
self.navigationController.toolbarHidden = NO;
I tried it and it worked. Just make sure that you put in the implementation file's viewDidLoad method.