How can I enable editing in the more tab? I can click on edit and see the items but I can't move them. Is there a option in UITabbarController?
Set tabBarController.customizableViewControllers to something, the easiest case would be:
tabBarController.customizableViewControllers = tabBarController.viewControllers;
after you set up the viewControllers.
You use the UITabBarControllerDelegate protocol when you want to augment the behavior of a tab bar. In particular, you can use it to determine whether specific tabs should be selected, to perform actions after a tab is selected, or to perform actions before or after the user customizes the order of the tabs. After implementing these methods in your custom object, you should then assign that object to the delegate property of the corresponding UITabBarController object.
First of all define outlets in the app delegate and attach them on Interface Builder, i assume you know how to do this:
IBOutlet UITabBarItem *tabBarItem1;
IBOutlet UITabBarItem *tabBarItem2;
IBOutlet UITabBarItem *tabBarItem3;
IBOutlet UITabBarItem *tabBarItem4;
Then your class, probably the view controller must be UITabBarControllerDelegate and use the following hook:
- (void)tabBarController:(UITabBarController *)tabBarController willEndCustomizingViewControllers:(NSArray *)viewControllers changed:(BOOL)changed
Hope that helps !
Related
I am using UITabBarController so that it can be displayed on all views once declared in delegate, but my requirement is that when any tab bar button clicked it should work like a button works on pushViewController:. Is it possible, can anyone guide here.
Thanks in advance.
you can just create a UINavigationController with toolbar hidden.
Then create a root view controller which has toolbar or custom view on bottom and add buttons on it.
Add targets to that buttons to push view controllers which you want.
This is not a typical behavior, however you can try as follows:
Declare a delegate (UITabBarDelegate) for your UITabBar, and implement the method -(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item to get notified of the user selecting tabs. In this method you will push a new view controller in the navigation controller's stack (or pop one that is not desired).
Depending on your needs, you may also need to manually set the currently selected tab bar item. To do so you can manipulate the tab bar's #property(nonatomic) NSUInteger selectedIndex. Be aware that changing this property will trigger another call to tabBar:didSelectItem: which may or may not be wanted.
I don't use UITabbarController,I just add the UITabbar in ViewController.I know when we use UITabbarController,we will create one array which contain viewcontrollers,so when we tap the tab,will show the specific viewcontroller,but the UITabbar just can add UITabbarItems,so how to connect the viewcontroller to the Tab?just like UITabbarController. thank you in advance.
Attach a UITabBarDelegate to your UITabBar:
self.tabbar.delegate = self;
// make sure you declared self to be a UITabBarDelegate in your header
Then implement:
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
// item is the selected tab bar item
}
Have your view controller implement the UITabBarDelegate protocol. Then set your tab bar's delegate property to point at your instance of your view controller. In your view controller class you can implement the tabBar:didSelectItem: method, which will get called whenever the user selects an item on your tab bar.
However, if you intend to use your tab bar to switch between different view controllers you should use a UITabBarController -- that's what it was designed to do.
I have an application which has 5 tabs on a TabBarController. For simplicity sake lets say they are Tab A, B, C, D, and E. Each tab takes the user to a TableViewController which is embedded in a Navigation controller. Each tab also has its own specific .h and .m files. The code for the most part is very similar between the 5 tabs. I want to do away with these 5 sets of class files and just use 1 set. This will make it much easier for me to make changes to the application (in 1 place instead of 5 places). How can I detect in the single implementation file which tab was selected? Once I know that I can put logic in place to render the tableview specifically for which tab was selected...
Another thing I should mention is that I need to detect the selected Tab in the TableViewController. The TabBarController is the point of entry for the application and I do not have a TabBarController subclass.
I tried this code in the TableViewController however it does not get accessed and/or used.
in .h file:
#interface MyController : UITableViewController <UITabBarDelegate>
in .m file:
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
//NSLog(#"selectedIndex: %d", self.tabBarController.selectedIndex);
NSLog(#"didSelectItem: %d", item.tag);
}
Easy, You already have the solution!
tabBarController
A parent or ancestor that is a tab bar controller.
(read-only)
#property(nonatomic, readonly, retain) UITabBarController *tabBarController
Discussion If the receiver is added to a tab bar controller, this property is the tab bar controller. If the receiver’s
navigation controller is added to a tab bar controller, this property
is the navigation controller’s tab bar controller. If no tab bar is
present or the receiver is a modal view, this property is nil.
That means that any viewController you add to a tab bar controller has this property filled in by the system.
Then in the view controller you want for the tab you implement viewWillAppear
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
NSUInteger selectedIndex = self.tabBarController.selectedIndex;
switch (selectedIndex) {
case 0:
//configure me
break;
case 1:
//configure me differently!!
break;
default:
break;
}
}
In light of the comments this property of tabBarController doesn't seem to be reliable.
The problem you describe sounds like something that could solved by subclassing. Make a subclass of UIViewController for the code in common with each tab and then subclass your subclass for each tabs viewController to make modifications unique to the tab.
Alternatively you could load each tab with the same class but a different xib. You can set properties on your view controller in the "user defined runtime attributes" section in interface builder. Then in the viewWillAppear block just check the property set by the xib on that instance.
If I understood you correctly, you have many choices:
- you may want to override the init method in your m file which I guess initializes a UITableViewCOntroller and pass an additional parameter to it depending on which tab you are in.
you may also want to add a tabid property to this class and set that when you are creating it for each tab (to something that shows which tab you are in).
you mat also use notifications (but it wont be the easiest or best solution, unless you have good reason not to use the first two)
I am sure there are lots of other ways.
How do I add using the InterfaceBuilder?
If you already have a UINavigationController, it's preferable to use its built-in toolbar.
In Interface Builder, check "Shows Toolbar" in your Navigation Controller's properties, and then use the toolbarItems attribute in your view controllers to populate the toolbar:
self.toolbarItems = [NSArray arrayWithObjects:...];
If you don't have a UINavigationController, you can drag a UIToolbar in from the library. I usually set its frame in the nib using the snap to guides feature. Then you can set the items with the method that #Can Berk Güder gives. Using UIBarButtonSystemItemFlexibleSpace and/or UIBarButtonSystemItemFixedSpace you can get a pretty good arrangement of buttons.
If you're doing it this way, just declare a UIToolbar* toolbar and set #property (nonatomic, retain) IBOutlet UIToolbar* toolbar in the interface of your view controller. In the implementation just #synthesize toolbar;. You can set the items in viewDidLoad and link the toolbar to your nib file by adding a new referencing outlet to 'toolbar' in File's Owner.
quick question - I have my "first view" which is going to be the ONLY view in my application. I've added a UITabBar to this view using Interface Builder. I am simply wanting to use this as a menu to control the contents of a scroll view.
For example, the user clicks on the first icon in the UITabBar - I get its tag, then based on that, will add a subview to the scrollview. This is working ok....
...but, I have been viewing a few tutorials on tabbars and it seems that 99% of the time they are used to control views. I simply want it to return my tags.
So my question is this: is what I am doing ok?? Can it be used for simply returning a value rather than changing a view? If this is common/OK practice, how on earth do I reference it?
I can get the selected item tag, but cannot actually reference the uiTabBar to make the first button selected. In my .h file, I tried to specify an IBOutlet for the controller, but I cannot link this in IB.
thanks for any info!
To receive notifications that a tab bar item has been clicked you need to modify your view controller to implement the UITabBarDelegate protocol and add an outlet for the tab bar. To do this, modify your declaration in MyViewController.h to something like this:
#interface MyViewController : UIViewController <UITabBarDelegate> {
UITabBar *tabBar;
...
}
#property (nonatomic, assign) IBOutlet UITabBar *tabBar;
Then implement the tabBar:didSelectItem method in MyViewController.m as follows:
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
NSLog(#"Tab clicked: %d", item.tag);
}
You must also set your view controller as the delegate of the tab bar in IB. (hint: connect up the 'delegate' outlet from the tab bar to File's Owner).
To access the tab bar from your view controller use the tabBar property and do things like:
self.tabBar.selectedItem = [self.tabBar.items objectAtIndex:0];
As to whether this is a good idea - why not? All the tutorials show a tab bar being used with a UITabBarContoller to switch views, but it is designed to operate as a stand-alone control as well. As long as you are not breaking any HIG rules then how you implement your interface switching is up to you.