Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Urgently: How can I change the selectedIndex tab? #43

Closed
TareqElMasriDev opened this issue Jul 29, 2015 · 10 comments
Closed

Urgently: How can I change the selectedIndex tab? #43

TareqElMasriDev opened this issue Jul 29, 2015 · 10 comments

Comments

@TareqElMasriDev
Copy link

When I use tabBarController?.selectedIndex it doesn't change the color in the animated tab, and I have tried using setSelectIndex() function but it didn't work, I really like this library but I have a deadline. Thanks in advance.

@cdf1982
Copy link

cdf1982 commented Aug 3, 2015

I have the same issue, have you figured out a solution?

@cdf1982
Copy link

cdf1982 commented Aug 3, 2015

It looks like I made it work, but I don't really like my own solution, so anyone more expert than me (meaning, everyone ;) is more than welcome to suggest a better way.

If you need to start your app in a different tab from the first one, it's pretty straightforward; in AppDelegate, inside application:DidFinishLaunchingWithOptions:

        let ramTBC = self.window!.rootViewController as! RAMAnimatedTabBarController

        ramTBC.selectedIndex = myDestination

        ramTBC.setSelectIndex(from: 0, to: myDestination)  // Because by default createCustomIcons() of RAMAnimatedTabBarController sets the selectedState for the item at index 0

If you need to switch programmatically from one tab to another during the execution of the app:

        let currentIndex : Int? = self.tabBarController?.selectedIndex

        self.tabBarController?.selectedIndex = myDestination

        if let ramTBC = self.tabBarController as? RAMAnimatedTabBarController,
           let current = currentIndex {

            ramTBC.setSelectIndex(from: current, to: myDestination)
        }

I hope it helps.

@cdf1982

@aalzanki
Copy link

aalzanki commented Aug 4, 2015

I just added a new method called setSelectedTabToIndex to the RAMAnimatedTabBarController class

func setSelectedTabToIndex(index: Int) {
    let items = tabBar.items as! [RAMAnimatedTabBarItem]

    let currentIndex = index
    if selectedIndex != currentIndex {
        let animationItem : RAMAnimatedTabBarItem = items[currentIndex]
        let icon = iconsView[currentIndex].icon
        let textLabel = iconsView[currentIndex].textLabel
        animationItem.playAnimation(icon, textLabel: textLabel)

        let deselelectIcon = iconsView[selectedIndex].icon
        let deselelectTextLabel = iconsView[selectedIndex].textLabel
        let deselectItem = items[selectedIndex]
        deselectItem.deselectAnimation(deselelectIcon, textLabel: deselelectTextLabel)

        selectedIndex = index
    }
}

@TareqElMasriDev
Copy link
Author

@cdf1982 @aalzanki Thanks guys, that was helpful, I'll test it and I'll inform you

@randeepbhatia
Copy link

The func setSElectedTabToIndex raises null pointer exception

@cdf1982
Copy link

cdf1982 commented Aug 5, 2015

Have you tried my approach?

Il giorno 05/ago/2015, alle ore 02:14, Randeep notifications@github.com ha scritto:

The func setSElectedTabToIndex raises null pointer exception


Reply to this email directly or view it on GitHub.

@TareqElMasriDev
Copy link
Author

@cdf1982 your soultion worked perfectly, thanks man

@0ber 0ber closed this as completed Dec 17, 2015
@sentiasa
Copy link

sentiasa commented Apr 8, 2016

@cdf1982 Thanks man! I tried the AppDelegate part and your solution worked perfectly.

However, my icon is using BounceAnimation but for the initial part (the AppDelegate part), the icon gets initialised with some offset below.. Is there a way to push it back up manually in the AppDelegate (to achieve it with a workaround) because on the other times later in the app, it behaves as expected. It's just the AppDelegate part. What may be the problem? How can I overcome this do you think? Thanks

@sesur
Copy link

sesur commented Jan 22, 2018

@cdf1982 I have found other way to set up you preferred tab when app starts, use:

func viewWillAppear(_ animated: Bool) 
{
        super.viewWillAppear(true)
         setSelectIndex(from: 0, to: yourPreferredTab)
    }

@wei-go
Copy link

wei-go commented Aug 29, 2018

Based on @aalzanki solution,
I modified the function as easier below

RAMAnimatedTabBarController.swift

open func setSelectedTabToIndex(index: Int) {
        guard let items = tabBar.items as? [RAMAnimatedTabBarItem] else {
            fatalError("items must inherit RAMAnimatedTabBarItem")
        }
        let currentIndex = index
        if selectedIndex != currentIndex {
            let animationItem : RAMAnimatedTabBarItem = items[currentIndex]
            let animationItemSelected : RAMAnimatedTabBarItem = items[selectedIndex]
            animationItem.playAnimation()
            animationItemSelected.deselectAnimation()
            selectedIndex = index
        }
    }

Usage
If your TabBarController is addressed under app.window as rootViewController,
you can make a class function to control your TabBarController

AppHelper.swift

class AppHelper: NSObject {
class func selectTabBarIndexTo(index: Int) -> Void {
        if let app = UIApplication.shared.delegate as? AppDelegate, let window = app.window {
            (window.rootViewController as! RAMAnimatedTabBarController).setSelectedTabToIndex(index: index)
        }
    }
}

and call above function from the other ViewController class

AppHelper.selectTabBarIndexTo(index: 0) // To tabIndex

@aalzanki Thanks for your answer to solved my issue 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants