I have created two instances of a MasterViewController derived from UIViewController class
_masterViewController = [[MasterViewController alloc] initWithNibName:#"MasterViewController_iPhone" bundle:nil];
// second instance with same class and duplicate nib view
_favItemMasterVC = [[MasterViewController alloc] initWithNibName:#"favMasterViewController_iPhone" bundle:nil];
Both the MasterViewController_iPhone & favMasterViewController_iPhone view are same.
Now I want to check which of the UIViewController is currently selected(eg:on tabbar).
How can i find the difference between both objects?
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
if ([viewController isKindOfClass:[_favItemListMasterVC class]]
{ // it is always called in both cases}
isMemberOfClass: // is also not working
How to check the difference?
Not sure I have understand what are you doing, but if _favItemListMasterVC and _masterViewController are pointing to the same VCs added to the UITabBar, you can check it simply comparing pointers
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
if (viewController == _favItemListMasterVC)
{
//the visible view controller is _favItemListMasterVC
}
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
if (viewController == _masterViewController)
{
}
else if (viewController == _favItemMasterVC)
{
}
}
I think you can use tag to check which is which. Tag is property of a UIView Set the tag value in the two xib files. And check the tag using code.
To compare objects you can also use:
if([viewController isEqual:_favItemMasterVC])
Related
Currently, Tapping on the same Tab (in which user is working), The App moves to the very first page of that Tab.
I want to disable the tap event on the Tab in which user is working currently.
Any Hint?
You tried tabBarController:shouldSelectViewController: delegate method? I hope that should help you.
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
id currentViewController = tabBarController.selectedViewController;
return (viewController != currentViewController);
}
If all the view controllers of the tab bar controller are UINavigationControllers, you should do it like this.
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
id nextVC = [(UINavigationController *)viewController topViewController];
id currentVC = [(UINavigationController *)tabBarController.selectedViewController topViewController];
return (nextVC != currentVC);
}
For Swift 4 the delegate method looks like this:
func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
return viewController != tabBarController.selectedViewController
}
use like below it will work
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
if(self.tabBarController.selectedIndex==[[self.tabBarController viewControllers] indexOfObject:viewController])
return NO;
else
return YES;
}
I would like to invoke a certain method whenever the user selects a different tab of a UITabBarController. The following works for actual tabs on the tab bar but not for the 'tabs' on the More controller:
-(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
[self doSomethingWhenAnotherVCIsSelected]
}
This method seems only to be called when a 'tab' is selected, including the 'more' tab. Whenever another VC on the 'more' tab is pushed, this is not called.
Is there any standard notification mechanism that can be used to detect if a VC was selected on the 'more' tab?
Call the method in the -viewWillAppear of the viewController.
Found the following workaround:
// subclass of UITabBarController
- (void)viewDidLoad
{
moreDelegate=self.moreNavigationController.delegate;
self.moreNavigationController.delegate=self;
...
}
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
[moreDelegate navigationController:navigationController willShowViewController:viewController animated:animated];
}
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
[moreDelegate navigationController:navigationController didShowViewController:viewController animated:animated];
[self tabBarController:self didSelectViewController:viewController];
}
I have read the Apple docs - http://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/TabBarControllers/TabBarControllers.html#//apple_ref/doc/uid/TP40007457-CH102-SW1 about creating TabBar programmatically. I want to detect the TabBar selection so I have used following delegate methods. I am not sure why but these methods don't get fired when I change the Tabs on my iPhone. Could anyone please provide some thought on what's going wrong here. It would be really helpful. Thanks.
- (BOOL)tabBarController:(UITabBarController *)tbController shouldSelectViewController:(UIViewController *)viewController
{
if (viewController == [tbController.viewControllers objectAtIndex:3] )
{
// Enable all but the last tab.
return NO;
}
return YES;
}
- (void)tabBarController:(UITabBarController *)tbController didSelectViewController:(UIViewController *)viewController {
if (viewController == [tbController.viewControllers objectAtIndex:self.appTabs.count] )
{
//do some action
}
}
Did you forget to set the delegate when you created the UITabBarController?
someTabBarController.delegate = self;
I have a tabBar with 4 tabs on it, and I want to perform some action when a specific tab is selected, so I have uncommented the UITabBarControllerDelegate in the xxAppDelegate.m
I also wanted to see the value that was being sent logged in the console - in order to test my "if" statement. However this is where I got stumped.
// Optional UITabBarControllerDelegate method
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
NSLog(#"%#", viewController);
}
The console dutifully logged any selected controller that had been selected, but in this particular format:
<MyViewController: 0x3b12950>
Now, I wasn't expecting the square brackets or the colon or the Hex. So my question is how do I format my IF statement? This is what I thought would work but I get an error mentioned further down.
// Optional UITabBarControllerDelegate method
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
NSLog(#"%#", viewController);
if (viewController == MyViewController)
{
//do something nice here …
};
}
... The error is "Expected expression before 'MyViewController'"
Anyone know how I should be doing this?
You need to compare to a specific view controller instance. For example, if the if statement should be true when the second tab is selected:
if (viewController == [tabBarController.viewControllers objectAtIndex:1]) {
// ...
}
Thanks that worked. I guess you have to know what object your comparing against first.
For anyone reading this the code supplied works, however you need to be careful that the text "tabBarController" in the example refers to the instance variable (the global one).
In order for your code to work your view controller needs to refer to a uniquely named local version as follows. Compare this to my original code.
// Optional UITabBarControllerDelegate method
- (void)tabBarController:(UITabBarController *)tabBarController_local didSelectViewController:(UIViewController *)viewController
{
//...
}
Hope this helps someone faced with the
Local declaration of 'tabBarController' hides instance variable
warning when trying to implement.
When comparing tabbarcontroller use self , like this:
if (viewController == [self.tabBarController.viewControllers objectAtIndex:1]) {
// ...
}
It will remove the warning.
I'm trying to figure out how to catch the event that controls the switch tabs on the
UITabBarController. How could I accomplish this?
Implement UITabBarControllerDelegate e.g. in your app delegate's applicationDidFinishLaunching
- (void)applicationDidFinishLaunching:(UIApplication *)application
{
tabBarController.delegate = self;
[window addSubview:tabBarController.view];
}
Then implement either:
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController;
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController;
The first method is called before the view switch and gives you a chance to 'veto' the view switch by returning NO
The second method is called after the view switch has taken place
If you are using storyboard, do this
in didFinishLaunchingWithOptions
UITabBarController *tabBar = (UITabBarController *)self.window.rootViewController;
[tabBar setDelegate:self];
Also in AppDelegate, keep <UITabBarControllerDelegate>
And then
-(void) tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
//Write your code here
}
Have a look at the following method in UITabBarControllerDelegate:
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
Tells the delegate that the user
selected an item in the tab bar.
Better late than never. In case of swift 4 you can do it in the following way.
tabBarViewController.delegate = self
And implement UITabBarDelegate in your class.
You will get the callback in
override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
//Stuff to do
}
Is UITabBarControllerDelegate what you're looking for, particularly -tabBarController:didSelectViewController:?