tabbarcontoller tab to the popviewcontroller - ios

In my app tabbar contain four tab on
first tab button is their on clicking on that button
it should jump to second tab i haved used.
self.tabBarController.selectedIndex = 1;
it works but requirement is that
on clicking on that button it should pop
to the first viewcontroller of second tab.
thank in advanced

You can check this when going to second tab bar in you appdelegate delegate method is called here you can poptorootviewcontroller
-(void) tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
Put following code inside this delegate method.
if ([viewController isKindOfClass:[UINavigationController class]])
{
[(UINavigationController *)viewController popToRootViewControllerAnimated:NO];
}

it working i haved used below code. on button click
self.tabBarController.selectedIndex = 1;
[[self.tabBarController.viewControllers objectAtIndex:1] popToRootViewControllerAnimated:NO];

Related

How can I click the tab bar to pop up a modal xcode

I want to click tab bar to pop up a modal view not just view controller.
(Like Instagram's camera tab bar to pup up camera view)
However when I drag from navigation controller to another view controller
I always got a black view when I run the app if I choose present modally.
So now, I can only choose root view controller.
Sorry for my poor English !
Can anyone understand and help me ?
The easiest way is to subclass uitabbarcontroller and use its delegate:
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
if ([viewController isKindOfClass:[SomeVC class]]) {
[self.navigationController presentViewController:vcToPresentModal animated:YES completion:NULL];
return NO;
}
return YES;
}
The main idea is to catch selection of some vc(it could be just empty UIViewController, connected with item, where should be modal presentation), cancel it,and show a modal vc.

Xcode: Detecting when a navigation controller implemented "back button" is pressed

Scenario
I have an app with a navigation controller. When the navigation controller pushes another controller onto the stack, in the upper left corner of the screen it shows the back button "<(title of the last view controller)".
What I need
I need something like (pseudo code)...
-(void)detectedBackButtonWasPushed {
NSLog(#"Back Button Pressed");
//Do what I need done
}
Question
Because this button is created by the navigation controller and I did not create this button in storyboards, how do I get the back button 'hooked up' to a method like this?
examples of what Ive tried for Oleg
-(void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
UIViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:#"notification"];
if (viewController == vc) {
NSLog(#"BACK BUTTON PRESSED");
}
}
Is this how I'm supposed to do it? Cause this doesn't work.
Use viewWillDisappear to detect this.
-(void) viewWillDisappear:(BOOL)animated
{
if ([self.navigationController.viewControllers indexOfObject:self]==NSNotFound)
{
[self backButtonPressed];
[self.navigationController popViewControllerAnimated:NO];
}
[super viewWillDisappear:animated];
}
-(void)backButtonPressed
{
NSLog(#"YEA");
}
Previously, I have solved this by setting the navigationBar leftItem to be a back button with a custom selector that dismisses the view along with whatever else it needed to do.
I might also suggest looking at the back button item and adding a target:self that is called on touch.

Navigate back to table of tab options every time 'more' option is clicked

I have a UITabBar application with 8 tabs in my iPhone app.
When I click on 'More' option, I get the remaining tabs and I navigate to the respective view.
If I click on the visible 2nd or 3rd tab and go back to the 'more' tab, I should again see the table of remaining tab options instead of the navigated subView.
So how shall I reset back to the 'more' tab table everytime 'more' is clicked?
I tried the following delegate method in AppDelegate.m
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
NSLog(#"controller class: %#", NSStringFromClass([viewController class]));
NSLog(#"controller title: %#", viewController.title);
if (viewController == tabBarController.moreNavigationController)
{
tabBarController.moreNavigationController.delegate = self;
}
if ([viewController isKindOfClass:[UINavigationController class]])
{
[(UINavigationController *)viewController popToRootViewControllerAnimated:NO]; //All viewControllers are UINavigationController classes in my app
}
}
#property(nonatomic, readonly) UINavigationController *moreNavigationController
is responsible for the navigation at the More item, you need to use it when you want to navigate to the root of the item:
[tabBarController.moreNavigationController popToRootViewControllerAnimated:NO];

Prevent UINavigationController from popping on double tap of UITabBarController

I have a UITabBarController in the root of my storyboard with three tabs. One of the tab is used to display the user information. I have set this tab to transition to the register/login page if the user has not registered. But the ViewController gets popped(and corrupts the Navigation Bar) when I double tap the tab icon. I cannot use the modal transition as it hides the TabBarController. What is the best solution to solve this issue?
You can use 'shouldSelectViewController' delegate of TabViewConroller to check if the selected viewController is same as the clicked one and skip it accordingly.
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
if(viewController == tabBarController.selectedViewController)
{
return NO;
}
}

iOS: Tabbar - load default state of tab

I need some idea or starting point to the following question:
I have an app which starts with an TabBarView - in some Tabs there are different Views / ViewControllers which are connected by seques.
If the active Tab is changed, i want the (now) open Tab to load the "Start"-View/ViewController of this Tab, not the View/ViewController which was last active on this Tab.
How can i do that?
I suggest you look at using the UITabBarDelegate method: tabBarController:didSelectViewController:
combined with the UINavigationController method: popToRootViewControllerAnimated:
So when the user selects a tab, you can ensure that the navigation begins from the root controller.
EDIT IN RESPONSE TO COMMENT:
It's not an ideal situation, but you can reference the UITabBarController in the app delegate. E.g.:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Get reference to Tab Bar Controller as the root view
UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
// Set Delegate
tabBarController.delegate = self;
return YES;
}
You can then implement the UITabBarDelegate method similar to:
-(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
// Pop to root if the selected controller is a navigation controller.
if ([viewController isKindOfClass:[UINavigationController class]]) {
[((UINavigationController *)viewController) popToRootViewControllerAnimated:NO];
}
}
I haven't tested this though!

Resources