How can I manage the user selection on the "more" view of a UITabBar? I have this code to manage the UITabBarItems selections:
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
if (!(viewController == tabBarController.moreNavigationController)) {
int index = tabBarController.selectedIndex;
[[DataManager sharedInstance] setCurrentTabbarIndex:index];
}
}
It works fine for the visible UITabBarItems, but when the user select some item from the "more" view I never get informed about that. Is there some way to catch the user item selection of the "more" view? Thanks!
The "more" view of a UITabBarController is handled separately from the other views. Apple's discussion on this subject says the following:
[The 'moreNavigationController'] property always contains a valid More navigation controller, even if a More button is not displayed on the screen. You can use the value of this property to select the More navigation controller in the tab bar interface or to compare it against the currently selected view controller.
Do not add the object stored in this property to your tab bar interface manually. The More controller is displayed automatically by the tab bar controller as it is needed. You must also not look for the More navigation controller in the array of view controllers stored in the viewControllers property. The tab bar controller does not include the More navigation controller in that array of objects.
Judging from that I would think that you can do something like:
int index = tabBarController.selectedIndex;
if (tabBarController.selectedViewController ==
tabBarController.moreNavigationController) {
index = -1; //assign some placeholder index for the "More" controller
}
A very late answer for a very old question, but here it is anyway, in case someone stumbles upon this.
The solution is to assign yourself as the delegate of the "more" navigation controller. You already have a class that adopts the UITabBarControllerDelegate protocol, so the .h file might look like this:
#interface MyDelegate : NSObject <UITabBarControllerDelegate, UINavigationControllerDelegate>
{
}
Wherever you are assigning your class as delegate, do this:
- (void) assignDelegate:(MyDelegate)myDelegate toTabBarController:(UITabBarController*)tabBarController
{
tabBarController.delegate = myDelegate;
tabBarController.moreNavigationController.delegate = myDelegate;
}
And finally, in your delegate class add this method:
- (void) navigationController:(UINavigationController*)navigationController didShowViewController:(UIViewController*)viewController animated:(BOOL)animated
{
// add code to handle the event
}
Note that none of your delegate methods are invoked when you change tabs programmatically.
The best approach I've found for this problem is to become the delegate for the UITableView which is the topViewController in the UITabBarController.moreViewController.
Implementing the didSelectRowAtIndexPath should solve the problem!
The best (and most secure) implementation for this is described by Stuart Sharpe in his blog: http://initwithstyle.net/2014/02/making-more-of-the-more-view/
Whatever view is being selected will receive viewWillAppear:animated:
Provide this in every view that's controlled by your tab bar, and you can thus extract the identity of the user's selection even when made from a "More" controller.
You can save the tab bar state right in this method, or you can provide your views with a reference back to the tab bar and notify it.
Related
Let me give an example what about what I want to do:
I have a tab that has 4 TabBarItems, so it contains 4 controllers. And there is a controller that must not get shown up in tab bar's icons, but it will get shown when a button inside one of these 4 controllers are touched. So when it get's shown, the tabbar must not have any selected tabs, every item must be deselected. It must be like a ghost controller that is not shown in tab icons but it's actually a controller that's in tab bar controller.
What's the best way to achieve this? Fyi, I don't want a modal dialog, the tabbar must always be visible underneath.
One thing which might help is knowing when a transition between tabs is happening. If you display your ghost view as soon as the transition happens, you might have enough control to do what you want.
Here's how I do that: first, sub-class UITabBarController to TabViewController (for example). In your TabViewController, include this method:
// Pass this message on to views so they know when transitions are occuring
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
UIViewController <UITabBarControllerDelegate> *obj;
for ( obj in self.viewControllers ) {
if ( [obj respondsToSelector:_cmd] ) {
[obj tabBarController:tabBarController didSelectViewController:viewController];
}
}
}
Set each of your tabbed view classes to adopt the UITabBarControllerDelegate protocol. Then, include a method like this in each class to "intercept" the transition events:
// This is called when a transition between tabs happens
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
if ( [viewController isEqual:self] ) {
// Transitioning to me
// Do stuff
}
else {
// Transitioning to someone else
// Do stuff
}
}
(note: there might be cleaner ways of implementing this, but it's been working fine for me...)
viewWillAppear is called both when going to the view and when coming back to the view from other views.
I want to select(highlight) and fade-out a cell only when coming back from other views.
Is there a delegate method to do this?
I'm using UINavigationViewController.
If you're on iOS 5, you can use these new properties:
These four methods can be used in a view controller's appearance
callbacks to determine if it is being presented, dismissed, or added
or removed as a child view controller. For example, a view controller
can check if it is disappearing because it was dismissed or popped
by asking itself in its viewWillDisappear: method by checking the
expression ([self isDismissing] || [self
isMovingFromParentViewController]).
- (BOOL)isBeingPresented __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_5_0);
- (BOOL)isBeingDismissed __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_5_0);
- (BOOL)isMovingToParentViewController __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_5_0);
- (BOOL)isMovingFromParentViewController __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_5_0);
In your code:
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
if (!(self.isMovingToParentViewController || self.isBeingPresented))
{
// animate
}
}
EDIT:
If you're using a UITableViewController, setting the property -clearsSelectionOnViewWillAppear to YES will do this for you. You only have to do it manually if you're using a regular UIViewController with a UITableView subview.
If you are targeting iOS 5, you can use [self isBeingPresented] and [self isBeingDismissed] to determine if the view controller is being added or removed from the nav controller.
I'm also suspecting that you could improve the logic of when you select/deselect the cell in your table view such that it doesn't matter whether the view controller is coming or going.
The usual way to do it is this: when someone selects a row in the table view in view controller A, it gets selected/highlighted and you push a new view controller B. When view controller B is dismissed, you animate the deselection of the table view row in viewDidAppear (so the user can see it fading out) in view controller A. You wouldn't worry about whether view controller A has just appeared or is re-appearing, because there would only be a selected table view cell in the appropriate case.
viewWillAppear is getting called when the view appears
after the viewDidLoad
after you dismiss or pull a view controller
You could change the viewWillAppear to the following
- (void) viewWillAppear:(BOOL)animated
{
static BOOL firstTime = YES;
if (!firstTime)
{
//Do your alpha animation
}
firstTime = NO;
}
In your UINav Controller you could create a "lastView" property and have each of your view controllers (that are controlled by your UINav Controller) set this property on "viewWillAppear"... in your target view... the one you want to do the highlighting and fading you could check this property of the UINav Controller and see if it's NIL or not.
That's just one way to do it. This wouldn't work if you pop up a modal or the like.
I'm using iOS 5 with Storyboard. My UITabBar is created using the Interface Builder. I have two similar items in my TabBar that is the same list, just with different type of item in it. What I've done, but looks weird to me, is setting a different "Tag" to each UITableView and in the viewDidLoad, and then assign the right type according to the Tag.
- (void)viewDidLoad
{
[super viewDidLoad];
if (self.tableView.tag == 1)
{
type = #"lent";
}
else if (self.tableView.tag == 2)
{
type = #"borrowed";
}
}
Any better way to do it? I'm not creating my UITabBar in code, so my AppDelegate is pretty empty! The type I set is just an attribute in one of my Core Data Entity, on a list I have Borrowed Items and on the other I have Lent Items, but they're the same entity.
You could expose the type as a property on your common view controller, and set it when the relevant tab bar item is selected (tabBarController:didSelectViewController:
from the UITabBarControllerDelegate protocol - your app delegate would be the tab bar controller delegate).
You would set this up as follows. Declare that your app delegate conforms to the UITabBarControllerDelegate protocol, then set it as the tab bar controller's delegate (you have to do this in code since the app delegate is not available to connect to in the storyboard). In your applicationDidFinishLaunching, add the following before you return YES:
UITabBarController *tbc = (UITabBarController*)self.window.rootViewController;
tbc.delegate = self;
Then implement the following delegate method:
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
switch (tabBarController.tabBar.selectedItem.tag)
{
case 1:
viewController.property = #"propertyA";
break;
case 2:
viewController.property = #"propertyB";
break;
}
NSLog(#"view controller is %#",viewController);
}
You'll need to cast the viewController variable to your actual view controller class, and also assign the relevant tags to the tab bar item of each view controller.
What you have there should work fine. Another option would be to have a common UIViewController super class that has all the functionality and then subclass that base class and provide an implementation of viewDidLoad that sets the appropriate type. Then in Interface Builder you can set the UITabBar view controllers as your appropriate subtypes.
The result would be the same, but it may be a bit more clear in IB what is going on because you don't have to rely on remembering the meaning of each numerical tag.
ok, i cant find the method called by UIViewController that presents a new view.
i have an app based on UINavigationController. Once i come to a view showing details of the object, i have 1 toolBar at the bottom so i can give some options to user.
When he press on barButtomItem, i want to show a new view but not changing the navigationbar, so if i press back button he goes back from detail view and not from new option.
i know method pushViewController but does not work as i want.
thx in advance!
edit: just to be a more bit more clear if i call
[[self navigationController]pushViewController:loteCompraViewController animated:YES];
i get a new view, related to the new controller but it also changes the navigationbar and that is not good for me.
Give your view controller of the "view showing details of the object" two methods: isShowingOverlayView and dismissOverlayView. In isShowingOverlayView, return YES if you're showing the "new view" because "he press on barButtomItem". In dismissOverlayView, hide the "new view".
Then make your own subclass of UINavigationController. In your subclass, override popViewControllerAnimated: to use those methods of your object-detail view controller, like this:
- (UIViewController *)popViewControllerAnimated:(BOOL)animated
{
if ([self.topViewController respondsToSelector:#selector(isShowingOverlayView)]
&& [(id)self.topViewController isShowingOverlayView])
{
[(id)self.topViewController dismissOverlayView];
return nil;
} else {
return [super popViewControllerAnimated:YES];
}
}
Use this subclass instead of the standard UINavigationController.
Trying to mimic/copy the built-in address book, specifically the behavior when editing a contact or viewing an existing contact's Info from inside the Phone app. When you navigate to another tab, the editing state is reset and the "New Contact" or "Info" view is popped so that when you come back to the Contacts tab, you are back at the root table view.
I have most of this working inside viewWillDisappear using setEditing: and popToViewController: however I get strange behavior when the user navigates from the Info view to the table view using the back button. Even if I pop to the root table view controller, it seems to be using the default UITableViewController class and not my subclass (e.g. standard selection behaviors instead of my overrides to push the detail view.)
Any hints? IPD
Here's some code to illustrate:
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
// This is to clean up from the colored bar in detail view
self.navigationController.navigationBar.tintColor = nil;
// These are to match the behaviour of Contacts app
[self setEditing:NO animated:NO];
// This is the tricky part: works when switching tabs, but not when back button was going to pop anyway!!
[self.navigationController popToViewController:rootViewControllerForTab animated:NO];
}
The -viewWillDisappear: method is not the best place for modifying the view controller stack for your navigationController because it is triggered both when you switch tabs and when a view is pushed on top of it.
I played around with this a bit and found that the best place for this is in the -[UITabBarControllerDelegate tabBarController:didSelectViewController:] method. So, first you need to designate an object to be the delegate for your tab bar (I used the app delegate). Bind the delegate property of your UITabBarController to an object implementing the UITabBarControllerDelegate protocol in code or in Interface Builder.
Then, implement the -tabBarController:didSelectViewController: method. The trick now is how to tell when your "address book" tab is being switched to. I kept track of the view controller for the tab in question using a property of type UINavigationController (the root view controller for the tab). After binding the tab1NavController property to the actual instance using Interface Builder, it can be used to compare to the viewController parameter to see what tab was just selected.
#interface Pop2RootTabSwitchAppDelegate : NSObject
<UIApplicationDelegate, UITabBarControllerDelegate> {
UINavigationController *tab1NavController;
}
#property (nonatomic, retain) IBOutlet UINavigationController *tab1NavController;
#end
#implementation Pop2RootTabSwitchAppDelegate
- (void)tabBarController:(UITabBarController *)tabBarController
didSelectViewController:(UIViewController *)viewController {
NSLog(#"[%# tabBarController:%# didSelectViewController:%#]", [self class],
tabBarController, viewController);
if (viewController == tab1NavController) {
NSLog(#"viewController == tab1NavController");
[tab1NavController popToRootViewControllerAnimated:NO];
}
}