iOS Storyboards - Re-use UIViewController - ios

I've got the following structure setup in my Storyboard.
I've got a TabViewController (circled in red) that shows a UIViewController via one of its tabs by doing a push (circled in blue).
I want to re-use that UIViewController from the TabViewController. I'd like to 'push' it but I don't really have a navigation controller so I may end up displaying it as a modal.
However, I'm not sure how to handle navigation back to the TabViewController since in this case there's no navigation bar. Any suggestions on the best way to handle this?
EDIT
Is there a way to insert a Navigation controller when its displayed directly from the TabViewController?

Why don't you put your view hierarchy like this:
UITabBarController -> UINavigationController -> BlueViewController
This UINavigationController should be put in the viewControllers property of the UITabBarController instance. If you do this, you can push and pop as many view controller as you want and you can also hide the navigation bar if needed.

Cancel button whose action is [self dismissViewControllerAnimated:YES completion:nil];

Related

How to maintain UITabBar with segue

So, I have an hierarchy which is(see image for better understanding): Navigation Controller -> Tab Bar Controller -> VC1,2,3,4,5
All good, all working.
Now, if I segue from one those VCs I lose my Tab Bar. Which I believe it is weird, since they all have a connection to the Tab Bar Controller VC.
Any way I can maintain it?
Thanks
you should not add UITabBarController inside UINavigationController, but you should add each UIViewController managed by UITabBarController inside a UINavigationController
like the following image

Going from NavigationController to TabBarController

So... I've got a ViewController that's being pushed onto a NavigationController. In interface builder I create a separate ViewController and Embed it into a TabBarController and it looks good in Interface Builder.
In my app, I'm trying to go from one of the ViewControllers in my NavigationView to the ViewController in the TabBarController. How would I do this the correct way? I can't just push the view onto the NavigationController, because the tab bar at the bottom won't show up.
Any help would be greatly appreciate.
I believe you're operating with the UINavigationController and UITabBarController in a backwards order to recommended best-practice.
Unless something has changed in the last year or two (which may have happened) the UINavigationController should never have a UITabBarController pushed onto it. If you are using a UITabBarController in your app, it should be the window.rootViewController, and the navigation controller being member of the UITabBarController's viewControllers array.
I'm trying to go from one of the ViewControllers in my NavigationView
to the ViewController in the TabBarController. How would I do this the
correct way?
In that structure, you'd assign your destination view controller as another element of the viewControllers array. Then, in my style, I'd send a NSNotification something like "LaunchOtherViewController" from your first view controller, and thus you have no need for the first view controller to know about the tab bar controller or second view controller. Then have some class that knows about the second view controller receive that notification, and update the selectedIndex of the UITabBarController to that of the second, destination view controller.
Hope that makes sense.
You need to push the TabBarController onto the view. You may need to set the selected view controller of the tab bar, but it's important the tab bar controller be actually pushed onto the navigation stack (or presented modally).

iOS how to go from xib file to a view controller in tab bar

i have a tab bar application
i am using storyboards and also navigation bar in my app
in my app
FirstViewController (in tab bar) with a button go to the SingleViewController (xib file and not on tab bar)
in SingleViewController there is a button too. and i want to go to one of the controllers which is on tab bar
in singleviewController's button method i have this code:
AylikGirisViewController *controller = [[AylikGirisViewController alloc] init];
[self.navigationController pushViewController:controller animated:YES];
it goes to the controller but i see a black screen and empty screen also moving to the AylikGirisViewController gets hard (i mean very slowly)
so i think my way is wrong
any ideas for the right way?
if you are using storyboard, you can simply drag another view controller onto the storyboard and give name as SingleViewController, you can easily push the SingleViewController onto the FirstViewController if you embed the FirstViewController in UINavigationController. Use storyboard segue to push the controller. Then you can call UITabBarController Delegate method to show the different viewController.

Segue to TabBar Controller

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];

pushViewController without navigationcontroller

I have a class that is of type UITableViewController.
This class has a member of type UINavigationBar that I use for adding in an edit button for the table view.
Using this method calling is invalid.
[self.navigationController pushViewController:controller];
How can I push a detail view onto the view after selecting a table row without wrapping my UITableViewController in a UINavigationController?
The closest alternative if you don't want to use navigation controller are modal view controllers.
[self presentViewController:controller animated:YES completion:nil];
This will slide the controller into your screen from bottom, or if you change controller's modalTransitionStyle it can flip over or fade in.
To dismiss the controller just call:
[self dismissViewControllerAnimated:YES completion:nil];
I would wrap the UITableView inside a UINavigationController and just hide the UINavigationBar.
[self.navigationController setNavigationBarHidden:YES animated:NO];
And then create a back button that pops the ViewController off the stack.
[self.navigationController popViewControllerAnimated:YES]
What could also be done is to use a Navigation Controller as usual and then hide it.
To hide the Navigation Controller using storyboards: select it and uncheck "Show Navigation Bar" in the attribute inspector. Others might suggest to hide the navigation bar in each controller, but the problem with that is that it will appear for a millisecond and then disappear.
You can't push a view controller onto a navigation controller if there is no navigation controller. If you are wanting to be pushing controllers and have it display the topmost controller and everything, just use a UINavigationController and be done with it.
You can push arbitrary UINavigationItems onto your UINavigationBar, and your bar's delegate will be notified when the user uses the back button so you can take appropriate action. See the documentation for more information.
It's true that without a UINavigationController you can not push view controllers. You rather present view controllers modally via UIViewController.present(_ viewControllerToPresent:, animated:, completion:)
But it's possible to create a custom segue to display the view controller as if it were a push (or any other animation you want), although it seems that using a UINavigationController just makes things easier.
Here are some related links to the documentation:
UINavigationController Class Reference
Customizing the Transition Animations
Presenting a Modal View Controller

Resources