Find which tab bar item is selected - swift

I'm looking for a way to find which tab bar item is select on my tab bar controller.
I've got 5 items and for one of them, I would like to show a "registration view" if the user is not logged in.
I've got all my verifications but I don't find the good way to check if the user tapped the fourth item on my tab bar.
Any ideas ? Thanks
self.tabBarController?.delegate = UIApplication.shared.delegate as? UITabBarControllerDelegate
func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
if viewController is CalculatorViewController {
print("Redirect to register view")
}
return true
}

func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
guard let index = tabBarController.viewControllers?.firstIndex(where: {$0 === viewController}) else {
return false
}
if index == 3 && !IS_LOGGED_IN{
/*** show registration ***/
return false //if you want to disable transition to the associated viewController against that tab
}
return true
}

You can try to use such thing (if you are using navigation controller, for sure)
override func viewDidLoad() {
super.viewDidLoad()
if let index = self.tabBarController?.selectedIndex, index == 3 {
// do things here
}
}
UPD.
Or even like so
override func viewDidLoad() {
super.viewDidLoad()
if !userLogedIn {
self.tabBarController?.selectedIndex = index // index is your tab bar item with login view
}
}

Related

TabBar Controller with custom action

I am new to swift. I am implementing tabbar controller in my Project and facing some design difficulty. My goal is when user click a tabbar item it should not navigate to another view controller. It should stayed in the current view and add a pop up view to the current view controller.I have tried but always it navigate to next view controller.
Create a UITabBarController subclass and use that class for your tab bar controller. Confirm to UITabBarControllerDelegate in the tab bar controller and return false in tabBarController shouldSelect method when you don't want to navigate to a view controller. Here you can show the popup view.
class TabbarController: UITabBarController, UITabBarControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
delegate = self
}
func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
if let navigationController = viewController as? UINavigationController,
navigationController.viewControllers.contains(where: { $0 is MoreViewController }) {
//show pop up view
return false
} else {
return true
}
}
}
Or you can add UITabBarControllerDelegate in one of its embedded view controller like this
class ViewController: UIViewController, UITabBarControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
self.tabBarController?.delegate = self
}
func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
if let navigationController = viewController as? UINavigationController,
navigationController.viewControllers.contains(where: { $0 is MoreViewController }) {
//show pop up view
return false
} else {
return true
}
}
}

Reset ViewController when tab bar icon is clicked

I have 3 items in my tab bar, each linked to a separate viewController, and I want them to reset each time I switch between any of these items.
How can I do this?
You can create a subclass for your tab bar controller and perform required actions in it.
class TabBarViewController: UITabBarController, UITabBarControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
self.delegate = self
}
func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
let viewController0: UIViewController? = tabBarController.viewControllers?[0] as? UIViewController
let viewController1: UIViewController? = tabBarController.viewControllers?[1] as? UIViewController
let viewController2: UIViewController? = tabBarController.viewControllers?[2] as? UIViewController
switch self.selectedIndex {
case 0:
// Refresh viewController1
// Refresh viewController2
case 1:
// Refresh viewController2
// Refresh viewController3
case 2:
// Refresh viewController0
// Refresh viewController1
default:
break
}
}
}

Opening a side menu on click on a TAB bar in SWIFT

I want to make something like below snapshots.
When I click on profile tab bar instead of opening a new view controller it shows a side menu. Is it something that has been handled on click of tabbar ?
if you want to achieve something like your screenShot then you are using a wrong library, because when you show your right viewController the front viewController go to left by amount of width of your right viewController, but anyways here is the code for what you need to do
first you need to put your viewController as delegate of your TabBarViewController and in func tabBarController(tabBarController: UITabBarController, shouldSelectViewController viewController: UIViewController) -> Bool you need to return false and call the method of SWRevealViewController to show right viewController rightRevealToggleAnimated(true)
class FirstViewController: UIViewController,SWRevealViewControllerDelegate,UITabBarControllerDelegate {
#IBOutlet weak var sliderControl: UISlider!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.revealViewController().delegate = self
self.tabBarController?.delegate = self
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
//self.view.removeGestureRecognizer(self.revealViewController().panGestureRecognizer())
//self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tabBarController(tabBarController: UITabBarController, shouldSelectViewController viewController: UIViewController) -> Bool {
//checking for specific viewController
if(viewController is DesiredViewControllerClass) {
self.revealViewController().rightRevealToggleAnimated(true)
}
return false
}
}
I hope this helps you, regards
You can use the tab bar delegate:
extension ViewController: UITabBarDelegate {
func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem) {
// Present hamburger menu
}
}

(Swift) need help swiping between view controllers

These are my view controllers:
I want to make it so that I can swipe between the three of them starting in the middle one. All of the tutorials I've found require you to start from scratch and don't show me how to connect the three I have. Does anyone have a step-by-step instruction of how to do this?
Any help would be greatly appreciated
First Create a class called PageViewController, drag a UIPageViewController in storyboard.For now lets set it as initial view controller from attributes inspector. Also from identity inspector set PageViewController as Class.
Call your three view controller for example StepZero,StepOne,StepTwo Also give them identifier in storyboard.
lets deep into coding now, so in PageViewController should subclass UIPageVIewController:
import UIKit
class PageViewController : UIPageViewController,UIPageViewControllerDataSource {
var selectedIndex = 1
override func viewDidLoad() {
dataSource = self
view.backgroundColor = UIColor.darkGrayColor()
// This is the starting point. Start with step zero.
setViewControllers([getStepOne()], direction: .Forward, animated: false, completion: nil)
}
func getStepZero() -> StepZero {
return storyboard!.instantiateViewControllerWithIdentifier("StepZero") as! StepZero
}
func getStepOne() -> StepOne {
return storyboard!.instantiateViewControllerWithIdentifier("StepOne") as! StepOne
}
func getStepTwo() -> StepTwo {
return storyboard!.instantiateViewControllerWithIdentifier("StepTwo") as! StepTwo
}
func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? {
if viewController.isKindOfClass(StepTwo) {
// 2 -> 1
return getStepOne()
} else if viewController.isKindOfClass(StepOne) {
// 1 -> 0
return getStepZero()
} else {
// 0 -> end of the road
return nil
}
}
func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController? {
if viewController.isKindOfClass(StepZero) {
// 0 -> 1
return getStepOne()
} else if viewController.isKindOfClass(StepOne) {
// 1 -> 2
return getStepTwo()
} else {
// 2 -> end of the road
return nil
}
}
// Enables pagination dots
func presentationCountForPageViewController(pageViewController: UIPageViewController) -> Int {
return 3
}
// This only gets called once, when setViewControllers is called
func presentationIndexForPageViewController(pageViewController: UIPageViewController) -> Int {
return selectedIndex
}
}
Let's say in Storyboard you have three viewControllers, you should set identifier for them from identity inspector as StepZero StepOne StepTwo for example and when you instantiate them you do :
func getStepZero() -> StepZero {
return storyboard!.instantiateViewControllerWithIdentifier("StepZero") as! StepZero
}
func getStepOne() -> StepOne {
return storyboard!.instantiateViewControllerWithIdentifier("StepOne") as! StepOne
}
func getStepTwo() -> StepTwo {
return storyboard!.instantiateViewControllerWithIdentifier("StepTwo") as! StepTwo
}
The selected index is the index you want to start with which is number 1. And to start with second view controller call getStepOne() in setViewControllers. if you want to start with view controller 3 use selected index 2 and call getStepTwo()...etc
Download Updated Sample : https://mega.nz/#!EQEFhbwS!0yoy5RvAliQNnjRevWo05wPWk7P08e8DVetRZdjg-ro
You can connect your view controllers by navigation controller . Just select one of your view controllers -> Editor(on the top bar of mac) -> Embed in -> Navigation controller.
Also if you want to swipe you can use a scroll view and only on view controller . Scroll view with content size of 3 view controllers can help you do the same.
Thank you
Follow as below image
Give storyboard identifier as shown in below image
On button click push new viewcontroller
var viewControllerObj=self.storyboard!.instantiateViewControllerWithIdentifier("your storyboard identifier")
self.navigationController!.pushViewController(viewControllerObj, animated: true)

opening UITabController with Segue from UIViewController - how can I show the 3rd tab as the default one?

I have a swift app and on my UIViewController I have a button. In my StoryBoard I attached the button to the UITabController and now when user clicks it - he gets redirected to it. But, by default, he is presented with the 1st tab. Is there a way of showing the 3rd tab instead?
This is the option of my segue:
Yes - but how complex it needs to be depends on what you're doing.
If you only ever go from the first UIViewController then you can simply add some code to the viewWillAppear or viewWillLoad function (remembering the index is zero-based)
override func viewWillAppear(animated: Bool)
{
self.selectedIndex = 2
}
If you have more than one entry point, you can use the prepareForSegue to set a flag in the tabBarController. In this example, I have two buttons on the UIViewController with tag values set as 100, and 200
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "TabBarSegue"
{
if let destinationVC = segue.destinationViewController as? myTabBarViewController
{
if sender!.tag == 100
{
destinationVC.jumpToTab2 = true
}
if sender!.tag == 200
{
destinationVC.jumpToTab2 = false
}
}
}
}
and then in the TabBarController, I have defined a flag jumpToTab2
class myTabBarViewController: UITabBarController
{
var jumpToTab2 : Bool = false
override func viewWillAppear(animated: Bool)
{
if jumpToTab2
{
self.selectedIndex = 2
}
jumpToTab2 = false // reset the flag before next time
}
}