View controller inside tab bar controller - ios

Hello I have tab bar controller with 5 elements. When I press the last one there are bunch of buttons which needs to show another view controller and then when I press second time to that tab bar button it needs to be revert its first condition (bunch of buttons) it is like a navigator. I tried to use following code:
let destinationVC = self.storyboard?.instantiateViewController(withIdentifier: "third") as? ThirdVC
self.tabBarController?.present(destinationVC!, animated: true, completion: nil)
However, view controller appears in a new screen and my tab bar controller is gone. How can I open my view controller above tab bar controller? Is it possible to do it ?

Try getting reference of the tabbar viewcontroller :
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let viewController = mainStoryboard.instantiateViewController(withIdentifier: "tabBarcontroller") as! UITabBarController
viewcontroller.selectedIndex = indexofyourvc i.e 0,1 etc
And push with the navigation controller :
self.navController?.pushViewController(viewController, animated: true )

Related

Add tab bar for all the screens iOS(swift)

I am working with UISplitViewController. I have UISplitViewController which contain UITabBarController. So I want to display tab bar in all the screen through out the application (Even in detail screen of UISplitViewController for iPhone). Please see the approach:
use segue to move from one controller to another controller or move via navigation so in the next controller your tabbar will be show
just like if u want to move from A to B Controller set stroyboard id ie "SecondVc"
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "SecondVc")
self.navigationController?.pushViewController(vc, animated: true)
or just make connection using storyboard

Navigation Bar Across Multiple Storyboards

I've separated my views into multiple storyboards to make managing the project as a team much easier. The only problem is that I want to have the navigation bar across all of my different view controllers. I embedded the first view controller in a navigation controller which added the navigation bar. In my second storyboard, I wanted to constrain a view to the navigation bar, however, since this view controller isn't embedded in a navigation controller, there isn't anything to constrain the view to. I put a navigation bar without a navigation controller to constrain to, however, when I perform the segue from the first storyboard, this results in two navigation bars. How do I make this so that I can constrain the view to the navigation bar without having two of them? Thanks in advance!
You can go from on view controller to another by three way:-
First using segue as you are already doing
second You can Present your view controller by
let storyboard = UIStoryboard(name: "MyStoryboardName", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "someViewController")
self.present(controller, animated: true, completion: nil)
Third You can Navigate View controller by
let storyboard = UIStoryboard(name: "MyStoryboardName", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "someViewController")
self.navigationController?.pushViewController(controller, animated: true)
I'm not sure your problem. I guess that you want to use ONE navigation bar to constrain many UIViewController in different Storyboard. Here is my answer.
Step 1: Set up Storyboard ID in each UIViewController
Step 2: push new UIViewController
func switchNextView() {
let storyboardName:String = "xxxxxx"
let storyboardId:String = "xxxxx"
let storyboard = UIStoryboard(name: storyboardName, bundle: nil)
let vc: UIViewController =
storyboard.instantiateViewController(withIdentifier: storyboardId) as!
UIViewController
self.navigationController?.pushViewController(vc, animated: true)
}

Go back previous ViewController from NavigationController in TabViewController

I got the problem is I cannot make go back previous ViewController of different storyboard from NavigationController in TabViewController.
I've already tried with
_ = navigationController?.popViewController(animated: true)
unfortunately, it does not work. And I know that it can be going back with segue but it's not good practice. Please let me know how to do it. Thanks. Following is my hierarchy.
I had the same issue. In this case I am on a different Storyboard and want to return to the Main Storyboard and present the first view controller. Since this controller is embedded in a Navigation Controller that is embedded in a Tab Bar controller, you must instantiate the Tab Bar Controller. Be sure to set the indentifier on the Tab Bar Controller as "TabBarController"... or whatever you like.
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "TabBarController") as! UITabBarController
self.present(controller, animated: true, completion: nil)

Swift ios send user to a specific ViewController in tab based application

I want to send a user to a specific ViewController in my app once a notification is clicked.
I now that I can do something like this:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let destinationViewController = storyboard.instantiateViewControllerWithIdentifier("Home") as? HomeViewController
presentedVC?.presentViewController(destinationViewController!, animated: true, completion: nil)
But my app has a tab bar and looks like this
Tab bar
tab1: navigationController -> VC1
tab2: navigationController -> VC2 -> HomeVC
tab: navigationController -> VC3
Each tab has a navigationController as a infront of it.
So how can I send the user to HomeVC? I must first select tab 2 then the navigation controller then push the user tvice:
tab2: navigationController -> VC2 -> HomeVC
And the other problem, if there any way to tell if the user is already in HomeVC? I dont want to send the user to the same VC if his already there.
You must have access to your UITabbarController in you UIApplicationDelegate or wherever you're handling the notification tap.
let tabBar:UITabBarController = self.window?.rootViewController as! UITabBarController //or whatever your way of getting reference is
So first you'll get the reference to UINavigationController in your second tab like this:
let navInTab:UINavigationController = tabBar.viewControllers?[1] as! UINavigationController
Now push your home view at second tab's navigation controller:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let destinationViewController = storyboard.instantiateViewControllerWithIdentifier("Home") as? HomeViewController
navInTab.pushViewController(destinationViewController!, animated: true)
And finally switch your tab to second to show the just pushed home controller
tabBar.selectedIndex = 1
Keep in mind, this answer assumes that your application has already set the tab bar as the root view controller of application window prior handling the notification tap.
Try something like this:
if let tabBarController = window?.rootViewController as? UITabBarController {
tabBarController.selectedIndex = 1 // in your case the second tab
}
The idea is to switch to get the tab bar instance and switch it to your desired tab (where you have your view controller).
The above code works in AppDelegate / you can easily call it anywhere by getting the tabBarController instance.
You can check which tab is selected by user with the method var selectedIndex: Int. You can check which view controller is present like this self.navigationController?.presentingViewController?.presentedViewController. This will solve your problem.

How to push viewcontroller correctly

This is my storyboard:
Short Description:
My App starts with the LaunchController
a modal segue shows the Reveal View Controller
this bring the Menu Controller and my Main Navigation Controller (ID
"NavController"; green Navbar) together. this will create a slide
menu. (Basic Code:
appcoda.com/ios-programming-sidebar-navigation-menu/)
my Main Navigation Controller shows a TableViewController.
this one have a menu button (3 Lines) which make the slide menu
visible
the plus icon willo push the last view Controller (ID: "VC1").
My Problem:
I would like to set quick actions for my app.
This code help works for that:
#available(iOS 9.0, *)
func handleShortcut( shortcutItem:UIApplicationShortcutItem ) -> Bool {
var succeeded = false
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let initialViewController = storyboard.instantiateViewControllerWithIdentifier("NavController")
self.window?.rootViewController = initialViewController
let navVC = self.window?.rootViewController as! UINavigationController
switch shortcutItem.type {
default:
navVC.pushViewController(storyboard.instantiateViewControllerWithIdentifier("VC1"), animated: false)
succeeded = true
break
}
return succeeded
}
This Code set the NavController as initial Controller und push the to VC1.
This works fine.
With the X icon in VC 1, i use the unwind function back to TableView.
The Problem: if i use the undwind function and fall back to tableview i can't open the slide menu. A Touch on the menu icon give no reaction.
The Problem can be, that i start the app with the quick action behind the Reveal View Controller.
How can I solve this problem?
Did you try to make the reveal view controller the entry point of your app and deleting the launch controller, because if you just want to go back to the main view you could just do this :
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateInitialViewController() as UIViewController!
self.presentViewController(controller, animated: false, completion: nil)
If you cannot could you explain the purpose of the launch controller?

Resources