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
Related
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
}];
}
So I have a root view with table view. I display the toolbar like this:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.navigationController.toolbarHidden = NO;
}
And I implement the setToolbarItems method:
- (void)setToolbarItems:(NSArray *)toolbarItems animated:(BOOL)animated
{
UIBarButtonItem *buttonItem;
buttonItem = [[UIBarButtonItem alloc] initWithTitle:#"Hello" style:UIBarButtonItemStyleDone target:self action:#selector(goBack:)];
self.navigationController.toolbarItems = [ NSArray arrayWithObject: buttonItem ];
}
The result is an empty tolbar. Why?
From the docs:
toolbarItems
The toolbar items associated with the view controller.
#property(nonatomic, retain) NSArray *toolbarItems
Discussion
This property contains an array of UIBarButtonItem objects and works in >conjunction with a UINavigationController object. If this view controller is >embedded inside a navigation controller interface, and the navigation >controller displays a toolbar, this property identifies the items to display in >that toolbar.
You can set the value of this property explicitly or use the >setToolbarItems:animated: method to animate changes to the visible set of >toolbar items.
In other words, try accessing it through the actual view controller, not it's navigation controller like so:
self.toolbarItems = [ NSArray arrayWithObject: buttonItem ];
But who calls your implementation of setToolbarItems?
You're supposed to call setToolbarItems on your own view, not re-implement it. Then, the NavigationController will find them in the instance variable and render them.
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.navigationController.toolbarHidden = NO;
UIBarButtonItem *buttonItem;
buttonItem = [[UIBarButtonItem alloc] initWithTitle:#"Hello" style:UIBarButtonItemStyleDone target:self action:#selector(goBack:)];
[self setToolbarItems: [ NSArray arrayWithObject: buttonItem ]];
}
I have a class that subclasses ABNewPersonViewController. As I understand, when the Done button in the navigation bar is clicked, the delegate method
- (void)newPersonViewController:(ABNewPersonViewController *)newPersonViewController didCompleteWithNewPerson:(ABRecordRef)person
gets called. But before entering the delegate method, the changes will be saved to address book.
What I need is, as soon as the save button gets pressed. I need to load a UIView with 2 buttons asking the user,
whether he wants the changes and
whether he should abort the changes made,
But this should be done before the changes are reflected in the address book. And only on the click of the first button in the UIView, that the changes should be saved in the address book.
On the click of the 2nd button, the view should disappear and I should return to the view controller class from where the UIView is loaded.
My question is, how will I load the view on save button click, before the changes are reflected in the address book
I have created a custom save button
UIBarButtonItem *okBtn = self.navigationItem.rightBarButtonItem;
UIBarButtonItem *saveBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:okBtn.target action:okBtn.action];
self.navigationItem.rightBarButtonItem =saveBtn;
[saveBtn release];
On the save button action, the control goes to the delegate method
- (void)newPersonViewController:(ABNewPersonViewController *)newPersonViewController didCompleteWithNewPerson:(ABRecordRef)person` .
I want the control go to my custom method, where I can load my UIView before the edits are saved in the address book.
Edit:
When I load the ABNewPersonViewController
ABPersonViewController *displayVcardViewController = (ABPersonViewController*)[self.navigationController visibleViewController];
ABRecordRef person = displayVcardViewController.displayedPerson;
EditAddressNewPersonDetailViewController *newVCardViewController = [[EditAddressNewPersonDetailViewController alloc] init];
newVCardViewController.displayedPerson = person;
newVCardViewController.newPersonViewDelegate = self;
newVCardViewController.isEditingMode = YES;
[self.navigationController setToolbarHidden:YES];
[self.navigationController pushViewController:newVCardViewController animated:YES];
[newVCardViewController release];
Isn't this strong reference already or else Where should I include the strong reference.
On
- (void)actionSave:(UIBarButtonItem *)sender {
if([[NSBundle mainBundle] loadNibNamed:#"MyView" owner:self options:nil]) {
[self.myView setFrame:CGRectMake(0, 0, 320, 480)];
[[UIApplication sharedApplication].keyWindow addSubview:self.myView];
UIActionSheet * action = [[UIActionSheet alloc]initWithTitle:#""
delegate:self
cancelButtonTitle:#"Do"
destructiveButtonTitle:#"Cancel"
otherButtonTitles: nil];
action.tag = 101;
[action showInView:self.view];
[action release];
}
}
I am loading a UIView with UIAlertView over it.
Update: Starting with iOS 7.0, ABNewPersonViewController is not subclassable anymore and this won't work.
First, keep a reference to the default rightBarButtonItem before overriding it.
If you're subclassing ABNewPersonViewController, your viewDidLoad would look like:
- (void)viewDidLoad {
[super viewDidLoad];
// Store the old button item into a custom property
// #property (nonatomic, retain) UIBarButtonItem *defaultRightBarButtonItem;
self.defaultRightBarButtonItem = self.navigationItem.rightBarButtonItem;
UIBarButtonItem *saveBtn = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemSave
target:self
action:#selector(actionSave:)];
self.navigationItem.rightBarButtonItem = saveBtn;
[saveBtn release];
}
And you call the default action on the default target in your custom action method:
- (void)actionSave:(UIBarButtonItem *)sender {
// Do what you want to do before the data is saved
// ....
// ....
// Trigger the default action
[self.defaultRightBarButtonItem.target
performSelector:self.defaultRightBarButtonItem.action
withObject:self.defaultRightBarButtonItem.target];
}
I'd like to display a ModalViewController from a bar button in the MainWindow.xib file. How would I do this? The basic code I'm looking to use is this:
-(IBAction)add {
myCustomViewController *add = [[myCustomViewController alloc] initWithNibName:#"myCustomViewController" bundle:nil];
[self presentModalViewController:add animated:YES];
[add release];
}
But where do I put it?
EDIT: I figured it out, in my navigation controller i put the following code in viewDidLoad:
UIBarButtonItem *addbutton = self.navigationItem.leftBarButtonItem;
[addbutton setTarget:self];
[addbutton setAction:#selector(add)];
and changed the function to:
- (void)add {
myCustomViewController *add = [[myCustomViewController alloc] initWithNibName:#"myCustomViewController" bundle:nil];
[self presentModalViewController:add animated:YES];
[add release];
}
Thanks for your help, Parth!
I fear that this is not possible.
You will have to put a viewController inside MainWindow.xib and put button on that viewController because you cannot add controls (like button in your case) on UIWindow.
It is required to be of type UIViewController or UITableViewController for you to be able to add UIControls to it.
Hope this helps you.
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.