Segue to TabBar Controller - ios

I'm using the following code to perform a segue to another view controller:
[self performSegueWithIdentifier:#"BackSegue" sender:self];
This works fine when the destination of "BackSegue" named segue is another view controller (one of the tabs, actually) but I need to display the tabs at the bottom so transitioning directly to this view controller won't work as there will be no tabs. Is it allowed/possible to segue to a tabbar controller? Is anything wrong with this specific code or would it be something else I'm doing?
Edit 1
The TabBar controller has no .m/.h files and is never declared programmatically, but I'm pretty sure the segue is set up correctly in the storyboard to the best of my knowledge (the same way it was set up earlier directly to the other viewcontroller).

You can segue directly to a UITabBarController. Just change the segue in your storyboard. When the segue occurs, it should load the tab bar controller, and consequently the tab bar at the bottom of the screen and the first view controller's view associated with the tab bar controller.

In order to segue to the specific tab in the tab bar controller:
You need to add the selectedIndex=1
Add these lines of code for segue:
UITabBarController *loadTabBar = [self.storyboard instantiateViewControllerWithIdentifier:#"TasksAppsTabs"];
loadTabBar.selectedIndex=1;
[self presentViewController:loadTabBar animated:YES completion:nil];

Related

Why is the tab bar not appearing throughout the app?

I have implemented a tab bar, but as I go through the app I am not seeing the tab bar. It disappears after I go to a certain page. This is how I implemented it. I have a tab bar connected to a vc which is embedded inside a navigation controller. So the hierarchy looks like this.
----UITabBarController
-------UINavigationController
-----------ViewController 1 with button to view controller 2 (I can see the tab bar)
----------------View Controller 2 (I can't see the tab bar)
It sounds like the segue that you get from view controller 1 to view controller 2 is a "present" segue, rather than a "push" segue. (If I recall correctly, Apple removed "push" segues from Storyboards recently.) Sadly, "present"ed view controllers appear in front of the navigation controller.
In order to do a "push" segue, you have to do it in code, e.g.:
- (IBAction)buttonTapped: (id)sender
{
ViewController2 *viewController = ...
[self.navigationController pushViewController:viewController];
}
i think you missed to initial tab bar as initial view controller

dismissViewControllerAnimated not working 4

Here is the scenario: I have 2 views, each embedded in a navigation controller. The first nav bar has 1 edit nav bar item, when tapped it goes to the 2nd VC. The 2nd VC nav bar has a cancel and save button, and when tapped should respond accordingly. I want to code the cancel action first, which will just call [self dismissViewControllerAnimated:YES completion:nil] but it is not doing anything...
I have also tried [self.navigationController popViewControllerAnimated:YES] and neither works, please help
BTW both segues are push segues
sounds like something off in you segues ...
i did a quick test in the following way:
create two view controllers in the story board ,each has it own navigation controller.
crate a segue from the first view controller to the second one.
2 in the first view controller i added a bar button that preform the segue from 2.
3 to the second view controller i added a bar button with an action that did : popViewControllerAnimated:YES..
all works fine...
though i don't really understand why you need 2 navigation controllers here....

Adding viewcontrollers to navcontroller with storyboard

I have a basic storyboard setup where I load my NavViewController, which then points to initial view controller. I then have several additional view controllers all daisy chained together via segues in a linear fashion. When I initially launch my application i run the following in my NavControllerViewController.m
(void)viewDidLoad
{
[super viewDidLoad];
NSArray * controllerArray = [self viewControllers];
NSLog(#"view controllers: %#", controllerArray);
}
The log only shows the very first root view controller (the one directly 'connected' to the nav controller). All over view controllers are missing from the stack. I was under the impression that if a view controller was on my storyboard that it would automatically be added to the nav controller?
If this is not correct, would a good alternative be to instantiate each VC, from the calling VC? For example, if I wanted to transition from VC1 to the VC2, would I put the following code in VC1:
UIViewController *vc2 = [self.storyboard instantiateViewControllerWithIdentifier:#"vc2"];
[self pushViewController:vc2 animated:YES];
Or possibly:
[self performSegueWithIdentifier:#"vc2Segue" sender:self];
They are all able to be reached by segues, but they are not instantiated or pushed onto the stack until you segue to them.
You could use the self perform segue tactic, or the push view controller, or, alternatively, if it is in response to a single button click, just control drag from that button to the next view controller and Xcode does all the rest of the work for you.
If I understand your question correctly, you have a storyboard setup looking similar to the below screen shot.When an application loads this storyboard it will definitely have only one view controller in the navigation controller stack and that will be the root view controller.
Because other view controllers are still not pushed into the navigation controller stack.
In the Viewcontroller-1, you could see a button some Action, I have created a push segue from that button to the Viewcontroller-2.Once you tap that button, second view controller will be pushed to the navigation controller stack.
If you print the viewcontrollers count now, you should get the count as 2.
Repeat the same in the View Controller-2, now you can see the count bumps to 3. Because now we have three view controllers pushed into the navigation controller stack.
Press the back button to pop the view controllers and could see the view controllers count coming down, that's because view controllers are now being removed from navigation controller stack.
As AdamG stated, the UIViewControllers will not be pushed on to the stack until you segue to them.
To set a segue, select a UIViewController and control+drag the connection to the target UIViewController. Under the Attributes Inspector tab set the Storyboard Segue Identifier.
To segue to a UIViewController use the method performSegueWithIdentifier:. Before performSegueWithIdentifier: is called prepareForSegue:sender: will be called. This is where you can pass any values that the next UIViewController needs. To check which segue is being called use the segue.identifier property in prepareForSegue:sender:. After that, you can access the destinationViewController property.
If you need to manually instantiate a UIViewController use instantiateViewControllerWithIdentifer:. The identifier can be set under the Identity Inspector tab in the storyboard.

iOS 6: How do I segue from inside a tableview embedded in a nav controller TO another VC?

I have an initial 'parent menu' view controller with a 'photos' button (more buttons to come like 'events').
From the Photos button I have a simple ctrl-drag segue to a navigation controller which has a tableview as it's first view(albums), collections view(thumbs), then imageview(fullsize).
The problem I am having is:
I want to be able to segue back to the initial simple 'parent menu' view controller from the first table view after the nav controller.
I tried:
dragging a button into the table view's top menu bar area (it was considered a UINavigationItem) and dragging a segue back to the menu... didn't work.
setup outlet from button in the tableview menu area to its parent view and calling [self performSegueWithIdentifier: #"BackToMenu" sender: self]; from there, didn't work.
dragging a button into the table view as a last item and ctrl+dragging it as a segue to the menu: didn't work.
tried a segue going back from the nav controller to the menu and manually calling it, but the outlet function connected to the 'menu' button I dragged into the tableview top back NEVER gets hit.
What am I missing?
I plan to have a few navigation controllers going out from the menu page in the storyboard from multiple buttons.
You don't need a segue for this. You can create a button in your table view and link it to an action in your table view which dismisses the currently presented view controller (I'm assuming your segue that presented the navigation controller stack in the first place was of a "modal" type):
[self.navigationController dismissViewControllerAnimated:YES completion:nil];
Alternatively, you can look into "Unwind Segues" but I haven't used these myself yet so wouldn't like to include details here.
You can drag a "Bar Button Item" to the left side of the first table view after the navigation controller. From there, do the control-drag back from the bar button item to the initial view controller, and choose a "modal" segue. While this answers your question, as #jrturton said in the comments below, this isn't optimized, because it will create a new instance.
If you want to do this programmatically (which will return to the existing instance), create an IBAction for the Bar Button Item on the table view controller. For example in iOS 6:
-(IBAction)returnToParentMenu:(id)sender {
[self dismissViewControllerAnimated:YES completion:nil];
}
or in iOS 5:
-(IBAction)returnToParentMenu:(id)sender {
[self dismissModalViewControllerAnimated:YES];
}
What I wound up doing was making the nav controller first before anything and then my menu page was first with buttons down each respective path.
on the above: The ctrl+drag didnt work from the nav bar button, kept totally blowing up.
I did not realize I could go from nav controller to a regular view controller first. thought it needed to go to table view first since when you drag out a nav it comes paired with a table.
If I had 10+ 'points' in this strange forum I'd post a screen shot of the storyboard.
Thanks all

UINavigationController shows black screen after pushing a view

I have a storyboard application with a navigation controller an two views controllers ('A', 'B').
In the Storyboard file:
Navigationcontroller is the initial view controller. view controller 'A' is connected to the Navigationcontroller as rootcontroller. View controller 'B' is in storyboard but not connected to any view controller.
when i programmatically try to push view controller 'B' onto the navigationcontroller from inside view controller 'A' with:
B *controllerB = [[B alloc] init];
[self.navigationController pushViewController:controllerB animated:YES];
all i get is a transition to a black screen.
i checked the navigationController property in view controller A at runtime and it´s not nil.
I do not instantiate the navigationController by myself, I let storyboard do the work (maybe that´s the problem). But I think it should be possible to "manually" push view controller to a navigation controller created by storyboard.
When I connect a segue from a button to 'B' in storyboard everything works fine.
Only programmatically it does not work, only shows a black screen inside the navigationcontroller.
Maybe someone could help me with this issue.
I haven't used storyboards yet so this answer is from a glance at the docs. It looks like you can't alloc/init a view controller from a storyboard. You need to use the instantiateViewControllerWithIdentifier: method of your UIStoryboard instance and then you can push the controller.
The accepted answer didn't work for me. I was already using instantiateViewControllerWithIdentifier to navigate to view controller in different storyboard. I am using xcode7.2 and Swift.
I am navigating from storyboard1, some view controller's button action.
Destination is to initial Navigation controller of Storyboard2.
Still I get black screen.
The problem was storyboard2's Navigation controller linked to 1st view controller was linked via show.
Solution:
Delete the link between Nav controller and 1st view controller. Now link it using root view controller. (Ctrl+Click on Navigation controller and drag it to the View controller and Select the option "root view controller")

Resources