IOS go to specific view from tabbar controller from other view - ios

I have a view with 3 buttons and a tabbar controller that contains 3 views. I am using the storyboard. I want go from my view to a specific view from the tabbar controller. When I create a segway to the destination view, the tabbar is not included.
The only way I find out is to create a segway to the tabbar controller itself, but then by default the first view is been shown.
Thanks in advance!

Yess!! I got the solution. Do the following:
In you're .h file:
#property (strong, nonatomic) UITabBarController *tabController;
In you're .m file:
#synthesize tabController;
tabController = [self.storyboard instantiateViewControllerWithIdentifier:#"tabbar"];
The selected index is the tab you want to go
tabController.selectedIndex = 1;
[[self navigationController] pushViewController:tabController animated:YES];

Related

Bottom bar got hidden when pushed from view controller

How to show the bottom bar when pushed from another view controller ?
Viewcontroller.m
ReminderViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:#"ReminderViewController"];
[self.navigationController pushViewController:vc animated:YES];
The viewcontroller does not contain any tabbar but ReminderViewController contains tabbar. However, when it was pushed, it did not show the tab bar at the bottom. Am I pushing it wrongly ?
Based on your naming - "ReminderViewController" - it sounds like you are loading and pushing the first view controller in your tab bar controller instead of the tab bar controller itself.
You need to subclass your tab bar controller. It doesn't need any special code... can be as simple as this:
// MyTabBarViewController.h
#import <UIKit/UIKit.h>
#interface MyTabBarViewController : UITabBarController
#end
// MyTabBarViewController.m
#import "MyTabBarViewController.h"
#interface MyTabBarViewController ()
#end
#implementation MyTabBarViewController
#end
Then, load and push your tab bar controller instead of the first tab's view:
MyTabBarViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:#"MyTabBarViewController"];
[self.navigationController pushViewController:vc animated:YES];

Storyboard - Pop to Root UIViewController Without UINavigationController?

I have a UIViewController hierarchy that consists of a Root View Controller, which segues to a UITabBarController which has several UIViewControllers under it. Each of those tab UIViewControllers has its own UINavigationController. There is no shared UINavigationController, and none applying to the Root View Controller. Now I have a situation in which I need to pop all the way back to the Root View Controller from one of the tab UIViewControllers. However, since the Root View Controller and the tab UIViewControllers do not share a common UINavigationController, I am unable to simply call [self.navigationController popToRootViewControllerAnimated:YES]. Is it possible to pop to the Root View Controller (or unwind my Segues back to the root programmatically) without a shared, common UINavigationController?
I had a similar problem once, try this
UIStoryboard *storyBrd = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UIViewController *controller = nil;
controller = [storyBrd instantiateInitialViewController];
self.view.window.rootViewController = controller;
This initial view controller is your RootViewController. Else you can also use the method
[storyBrd instantiateViewControllerWithIdentifier:<View Controller's Restoration ID>]
Here's how I understand the vc arrangement:
|-navvc
| |--rootvc-(pushes)-stack...
"root" vc-(presents)->tabvc--|
|-navvc
|--rootvc-(pushes)-stack...
Its easy to unwind all of this so long as you're holding the handles to the right stuff. The navigation vcs within the tabs can be accessed by any view controller on their stacks with self.navigationController.
You'll need to have ahold on either the tab bar vc or the one you call "root" vc, also. You can arrange that with a property on your app delegate.
The only other tricky thing to remember is that the transition from the "root" vc to the tab bar is a present, not a push, so it must be undone with a dismiss, not a pop. To express this in code, lets say you've got a handle to the tab bar vc on your app delegate....
#interface AppDelegate : UIResponder <UIApplicationDelegate>
#property (strong, nonatomic) UIWindow *window;
#property (strong, nonatomic) UITabBarController *tabBarVC;
- (void)popEverything:(BOOL)animated;
#end
// AppDelegate.m
// get the tab bars tabs (which are presumed to be navigation vcs)
// dismiss the tab bar vc (which was presumed to be presented)
// iterate the navigation vcs, popping all of them to root
- (void)popEverything:(BOOL)animated {
NSArray *viewControllers = self.tabBarVC.viewControllers;
[self.tabBarVC dismissViewControllerAnimated:animated completion:^{
for (UINavigationController *navVC in viewControllers) {
[navVC popToRootViewControllerAnimated:animated];
}
}];
}
Caveats apply: still guessing a little about your vc arrangement, and haven't tested the foregoing.

Move to already allocated view?

Ad the title says, I'm wondering how I move back to a already allocated view. In this case I have three views, Mainmenu, Main_game_view and game_over_view. I want to move back from game_over_view to mainmenu without reallocating it. Also note that I'm using xib files
How do I do that?
If you use storyboard, you can create a segue by control-dragging.
If you move from viewController1 to viewController2 by [viewController1 presentViewController:viewController2 animated:YES completion:nil];, you can use [viewController2 dismissViewControllerAnimated:YES completion:nil]; to go from viewController2 back to viewController1.
If you use a UINavigationController and push viewController2 by [someNavigationController pushViewController:viewController2 animated:YES];, then you can do [someNavigationController popViewControllerAnimated:YES]; to pop the view controller and go back to the first view.
If it's multiple views presented on the same view controller, then you can remove the views from their super view using [someView removeFromSuperview] and leave the first view only.
Hope it helps :)
========
Edit:
You can create a reference of your Mainmenu in your Game_over_view view controller:
In Main_game_scene view controller's .h file, create #property (strong, nonatomic) UIViewController *mainMenuViewController;. After Mainmenu creates Main_game_scene view controller, do main_game_scene.mainMenuViewController = self;.
In Game_over_view view controller's .h file, also create #property (strong, nonatomic) UIViewController *mainMenuViewController;. After Main_game_scene creates Game_over_view, do game_over_view.mainMenuViewController = self.mainMenuViewController;.
Now you have a reference of Mainmenu in Game_over_view, just call [self.mainMenuViewController dismissViewControllerAnimated:YES completion:nil];.

How to get tabBarController in one of its viewController when using storyboard?

I make a Tabbed Application using storyboard template, two view controllers are embedded.
This is what I want to do: in the first viewController, let TabBar to select the second viewController programmatically.
The first viewController is a tableViewController, shows a list of items, and each item will push to a detailViewController. In the detailViewController, I edit some information and save the item. Then I want app to show the second ViewController, which is a tableViewController shows saved item.
Usually, we can use [TabBarController setSelectedIndex:1]; to select the second viewController.
However, since this is a storyboard template application, so many code are hidden behind. So I cannot get the TabBar instance in the first viewController, and use setSelectedIndex method.
This is what confuses me...
And now, I have found the solution for this problem. My answer is below.
I have figured out how to solve this problem.
First I add new a class: MyTabBarController.
Then, in storyboard, select the Tab Bar Controller, in identity inspector panel, set the custom class to this new class.
For the first viewController class, add a property
#property (nonatomic, weak) UITabBarController *tabBarController;
Then add - (void)viewDidAppear:(BOOL)animated in MyTabBarController class:
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
UINavigationController *navigationController = [self.viewControllers objectAtIndex:0];
FirstViewController *firstViewController = (FirstViewController *)navigationController.topViewController;
firstViewController.tabBarController = self;
In this way, I pass the tabBarController instance to the firstViewController, so, in the firstViewController, I can call [tabBarController setSelectedIndex:1];
Storyboard gives me a visual interface, however, it hides so many things behind.

iOS: popping up a subview in a tabview

Newbie question for iOS -- I'm really confused with how navigation view works in a tabview.
Right now I have a tabview that has 2 views. In the second tab I have a button. When the button is clicked I'd like a new window to show up with some information, and the new window needs a Back button on top that goes back to the second tab.
I followed some tutorials and put a NavigationController in secondTab.xib, added the line
#property (nonatomic, retain) IBOutlet UINavigationController *navController;
to secondTab.h, and
NewWindowController *newWindow = [[NewWindowController alloc] initWithNibName:#"NewWindowController" bundle: nil];
[self.navController pushViewController:newWindow animated:YES];
NSLog(#"clicked");
to my button implementation for -(IBAction) click: (id)sender
When I clicked the button in my second tab, the log shows "clicked" but my view is not changing.
Is there some setting I need to change for File's Owner/Navigation Controller outlets/referencing outlets etc...?
Thanks!
You don't want a property for the UINavigationController, you want to push onto the current navigation controller like so:
NewWindowController *newWindow = [[NewWindowController alloc] initWithNibName:#"NewWindowController" bundle: nil];
[self.navigationController pushViewController:newWindow animated:YES];
NSLog(#"clicked");
When a UIViewController is associated with a UINavigationController (i.e. it's part of a navigation controller hierarchy) then its navigationController property will be set, so you can access it like I've shown.

Resources