I have a TapBarViewController with three Taps in my project.
Now I am trying to put a normal FirstViewController in front of the TapBar ViewController when the application launches.
The FirstViewController should have three buttons for one of the individual Taps of the TapBarViewController.
How can I show the individual Taps of the TapBarViewController by pressing one of the three buttons?
Screenshot
This is the exact answer you are looking for.
Steps:
Add a UIViewcontroller and embed with UINavigationViewController (Editor -> EmbedIn -> NavigationController).
Add Three button + 1 Button (Navigate to TabViewController).
Drag and drop UITabBarController to the storyboard it comes with one default ViewController and place a UILabel named as 'ButtonOneClicked'.
Create Two more ViewControllers and named as 'ButtonTwoClicked' and 'ButtonThreeClicked'.
Connect 'ButtonOne' with FirstViewController and set segue as Show and repeat the same for rest two.
Click on each segue and provide an unique Identifier name.
Then connect the UITabBarController with each UIViewController and choose viewcontrollers
Create a ButtonAction to the Tab Button.
Copy paste the below code in that action.
#IBAction func tabClicked(_ sender: Any) {
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.window = UIWindow(frame: UIScreen.main.bounds)
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let initialViewController = storyboard.instantiateViewController(withIdentifier: "Tab") as! UITabBarController
appDelegate.window?.rootViewController = initialViewController
appDelegate.window?.makeKeyAndVisible()}
Hope this helps you!!
Related
Sorry, if it is an essential question.
The scheme is the following:
enter image description here
I want the following:
User taps last Tab
User goes to some ViewController (different from ViewController, actually connected with last Tab)
How to do this (without using segue)?
Thanks a lot in advance!
You can go with instantiateViewController with push when load your tab and push one viewController to another like below:
let next = self.storyboard?.instantiateViewController(withIdentifier: "nextVC")as! nextVC
self.navigationController?.pushViewController(next, animated: true)
You can assign ViewController in you UITabBarController
let storyboard = UIStoryboard(name: "Main", bundle: nil)
// create view controllers from storyboard
// Make sure you set Storyboard ID for both the viewcontrollers in
// Interface Builder -> Identitiy Inspector -> Storyboard ID
let clockViewController = storyboard.instantiateViewControllerWithIdentifier("ClockViewController")
let stopWatchViewController = storyboard.instantiateViewControllerWithIdentifier("StopWatchViewController")
// Set up the Tab Bar Controller to have two tabs
let tabBarController = UITabBarController()
tabBarController.viewControllers = [clockViewController, stopWatchViewController]
// Make the Tab Bar Controller the root view controller
window?.rootViewController = tabBarController
I have what I am calling a one time onboard set of UIViews that ask the user a couple questions to configure my iOS app correctly.
When the user has answered the onboarding questions I would like to segue to the UITabBarController Home screen, this is the same screen that will always appear every subsequent time the app opens after onboarding is complete. I do not have an Initial UIViewController set in my Storyboard, this, in my AppDelegate is how I go to the correct view, every time the app loads:
self.window = UIWindow(frame: UIScreen.main.bounds)
let storyboard = UIStoryboard.init(name: "Main", bundle: nil)
if Settings.sharedInstance.onboardComplete{
let vc = storyboard.instantiateViewController(withIdentifier: baseTabBarStoryboardIdentifier) as! UITabBarViewController
self.window?.rootViewController = vc
} else{
self.window?.rootViewController = storyboard.instantiateViewController(withIdentifier: onboardStoryboardIdentifier) as! OnboardNavigationController
}
self.window?.makeKeyAndVisible()
This works fine, and I am able to load the UITabBarViewController which contains all my app's primary UIViews without any issues.
I would like to achieve this same "segue" from a UIButton TouchUpInside action. This segue would occur in a set of UIViews in a UINavigationViewController that has no connection to the main app UIViews contained in aforementioned UITabBarViewController, here is a screenshot of my Storyboard to give some context to this statement, you can see the onboarding UIViews circled in red, the main app UIViews contained in a UITabBarViewController are circled in blue:
I have tried the same code as above in my AppDelegate in my UIButton action outlet implementation, I create a UIWindow instance, I get the Storyboard, I instantiate the UITabBarViewController, I set the UIWindow rootViewController. I am not able to figure out a way to transition to my main app UITabBarViewController after the user completes the Onboarding questions
Try this
let appDelegate = UIApplication.shared.delegate as? AppDelegate
let mainStoryboard = UIStoryboard(name: "Main", bundle: nil)
let homeController = mainStoryboard.instantiateViewController(withIdentifier:baseTabBarStoryboardIdentifier) as! UITabBarViewController
appDelegate?.window?.rootViewController = homeController
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.
My application has a ViewController i.e. the first screen that opens when app is started. I added a UITabBarController to the application through mainstoryboard. I changed the title of UITabBarController to "myTabBarController". Now how do I access this TabBarController from the viewDidLoad function of already existing ViewController?
amazed! Why do you need view controller if you want to load UITabBarController from viewDidLoad function of view controller???
However solution can be like this:
You can make segue: right click in view controller then drag to tabBarController and select segue "show".
But, better would be if you embed UITabBarController to view controller which loads your TabBarController at the beginning of your app.
1: Select View Controller
2: Go to [Editor] in menu bar of xcode
3: Select Embed In
4: Select Tab Bar Controller
and you are done :)
other solution would be:
delete the ViewController from storyboard
select tabBarController that you added.
go to "attribute inspector" in left pane and select "Is initial View Controller"
ADDED
1. Give identifier to TabBarController like given by 張家齊
2. Add button in ViewController and make Action of it in ViewController's class (to make action. right click the added button and drag to ViewController class and select Action instead of Outlet.)
3. Now add following codes in that action:
let storyBoard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyBoard.instantiateViewControllerWithIdentifier("myTabBarController") //vc is instance of TabBarController.
self.presentViewController(vc, animated: true, completion: nil)
DONE :)
In AppDelegate.swift
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let main = storyboard.instantiateViewControllerWithIdentifier("myTabBarController") as! myTabBarController
self.window!.rootViewController = main
self.window!.makeKeyAndVisible()
return true
}
And you should set Identifier of myTabBarController in your Main.storyboard.
There is a great blog post over at http://www.appcoda.com/tag/swrevealviewcontroller/ that goes in to setting up SWRevealViewController which is a great component for slide out side menus (https://github.com/John-Lluch/SWRevealViewController)
Unfortunately, there are no swift examples of how to perform a manual segue.
Took a cleaner approach to storyboard support. SWRevealViewControllerSegue is now deprecated and you should use SWRevealViewControllerSegueSetController and SWRevealViewControllerSeguePushController instead.
I've tried something along the lines of:
let navigationController = self.window?.rootViewController as! SWRevealViewController;
let viewController = navigationController.storyboard?.instantiateViewControllerWithIdentifier("ImportFileSelect") as! ImportFileSelect
navigationController.showViewController(viewController, sender: self)
This doesn't work though. Any ideas? I've trawled the web for swift examples, my next step is to learn objective c!
In order to work you'll need to following steps:
You need to instantiate the SWRevealViewController and then attach it
to the root controller.
Then instantiate the destination controller.
Then create a navigation controller and set the destination
controller as the rootViewController
Finally push the navigation controller with SWReveal
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let sw = storyboard.instantiateViewControllerWithIdentifier("SWRevealViewController") as! SWRevealViewController
self.view.window?.rootViewController = sw
let destinationController = self.storyboard?.instantiateViewControllerWithIdentifier("StoryboardID") as! NameOfViewController
let navigationController = UINavigationController(rootViewController: destinationController)
sw.pushFrontViewController(navigationController, animated: true)
I've kind of made some progress. I can load in a new view controller, but it doesn't animate in anyway. The code to do this, on a button click is:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("TARGET_VIEW_CONTROLLER") as! UIViewController
var rvc:SWRevealViewController = self.revealViewController() as SWRevealViewController
rvc.pushFrontViewController(vc, animated: true)
Download SWRevealViewController project's zip form Github.
Drag SWRevealViewController.m and SWRevealViewController.h files from zip (SWRevealViewController folder) to your project, and Click “Yes” to the prompted message "Would you like to configure an Objective-C bridging header?"
Take a look at storyboard in RevealControllerStoryboardExample2 project. Design your storyboard with this example.
This is not how it should be done, but at least i found a way to do it.
In the TableViewController where u have the slideOut menu do something like:
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if(indexPath.row == <some position> ){
let vc: AnyObject! = self.storyboard?.instantiateViewControllerWithIdentifier("YOUR_DESTINATION_IDENTIFIER")
self.showViewController(vc as! SWRevealViewController, sender: vc)
}
The one that satisfy the condition will segue with default animation, not like the slide out menu normally does.
After, in storyboard, do a normal Reveal View Controller Push controller, and as long as it doesn't satisfy the condition it will exit the slide out menu normally