0

How can i set an action for my UITabBarItem ? Can i connect it from storyboard to code somehow? or programmatically? And if so how can it be done?

My hierarchy is UITabBarController -> UINavigationController -> UITabBarItem

Sorry for the noobish question but i really need an answer for this. And I couldn't find anything regarding this online.

Using Swift 3 and Xcode 8

Thank you!

i tired Mannopson's Answer (didn't work):

class BarsViewController: UITableViewController,UISearchResultsUpdating,UISearchBarDelegate,UISearchDisplayDelegate,UITabBarControllerDelegate{

override func viewDidLoad() {
super.viewDidLoad()


self.tabBarController?.delegate = self

        }

func tabBarController(tabBarController: UITabBarController, didSelect viewController: UIViewController) {
            if viewController.tabBarItem.tag == 1 {
                self.tableView.setContentOffset(.zero, animated: true)
            }
        }
        }
                extension UITableView {
                func scrollToTop(animated: Bool) {
                    setContentOffset(CGPoint.zero, animated: animated)
                }
            }

silentBob Answer Didn't Work for me :

class BarsViewController: UITableViewController,UISearchResultsUpdating,UISearchBarDelegate,UISearchDisplayDelegate,UITabBarControllerDelegate{


    override func viewDidLoad() {
        super.viewDidLoad()


    self.tabBarController?.delegate = self
}

func tabBarController(tabBarController: UITabBarController, didSelect viewController: UIViewController) {
        if viewController.tabBarItem.tag == 1 {
            tableView.scrollToRowAtIndexPath(NSIndexPath(forRow: 0, inSection: 0), atScrollPosition: .Top, animated: true)
        }
    }

}

i also tried Tushar Sharma's answer and it didn't work as well.

What Im trying to do is when you are scrolled down in the tableView you will tap the current Tabbaritem the tableView Will scroll to top just like intagram.

Newbie Questions
  • 463
  • 1
  • 11
  • 31

3 Answers3

0

Basically you do this:

1) Make sure your class confirm to the UITabBarDelegate

2) Set tags in IB for each tab bar item

3) Implement the didSelectItem method, something like this:

 class yourclass: UIViewController, UITabBarDelegate {
        func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem) {
           if(item.tag == 1) {
       //your code for tab item 1
    }
    else if(item.tag == 2) {
       //your code for tab item 2
    }
        }
    }
Tushar Sharma
  • 2,839
  • 1
  • 16
  • 38
0

I think you'll need to setup UITabBarControllerDelegate protocol. Try this:

class YourClass: YourSuperClass, UITabBarControllerDelegate {

    func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
        if viewController.tabBarItem.tag == 1 {
            self.tableView.setContentOffset(.zero, animated: true)
    }
}

    override func viewDidLoad() {
        super.viewDidLoad()

        self.tabBarController?.delegate = self
    }
}
Mannopson
  • 2,634
  • 1
  • 16
  • 32
0

If you only wish to scroll to top, there is a builtin system option for this, try tapping on status bar.

Edit:

Swift 3:

class ViewController: UITableViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func viewWillAppear(_ animated: Bool) {
        self.tabBarController?.delegate = self
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

extension UITableViewController : UITabBarControllerDelegate {
    public func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
        tableView.scrollToRow(at: IndexPath(row: 0, section: 0), at: .top, animated: true)
    }

}

Swift 2:

tableView.scrollToRowAtIndexPath(NSIndexPath(forRow: 0, inSection: 0), atScrollPosition: .Top, animated: true)


extension UITableViewController : UITabBarControllerDelegate {
    func tabBarController(tabBarController: UITabBarController, didSelect viewController: UIViewController) {
        tableView.scrollToRowAtIndexPath(NSIndexPath(forRow: 0, inSection: 0), atScrollPosition: .Top, animated: true)
    }
}

Oh, that was a nice lunch :)

Here is an extension for UITableViewController you can use if you wish to scroll to top on every view controller, i'm still playing with it, but i think you can use it.

silentBob
  • 170
  • 9
  • Yea i know but instagram has both methods tapping the status bar and the tabbaritem.... i want it as well. – Newbie Questions Mar 12 '17 at 02:33
  • Here, i've edited my answer, i think this should work for you. Also you have an option to use property instead of tags to get the right item in tabbar. Also scrollRectToVisible is an option. – silentBob Mar 12 '17 at 02:47
  • can you translate to swift 2.2 ? I'm having troubles to translate that. will be helpful , thank you – Newbie Questions Mar 12 '17 at 10:06
  • Sure, but i'd advise you to switch to Swift 3 if you can. It won't go backwards, and Swift 4 is in progress already. I think syntax was like this: scrollToRowAtIndexPath(NSIndexPath(forRow: 0, inSection: 0), atScrollPosition: .Top, animated: true) – silentBob Mar 12 '17 at 10:45
  • I know ... sadly i barely know swift 2.2 and thats what i started with , I'm the most comfortable with this syntax.... i will need a loot of help moving my project to Swift 4 :( im trying to learn by your answers and videos online . Thanks for the translation , i will try it now – Newbie Questions Mar 12 '17 at 10:48
  • You have some time until that, but it was quite a jump from 2 to 3. Swift 4 shouldn't be that much of a change, that is if you're jumping from Swift 3 ;) – silentBob Mar 12 '17 at 10:49
  • Updated Question : this is what i have now and it doesn't work for me :( – Newbie Questions Mar 12 '17 at 10:53
  • There are a lot of sources for learning, here is a link of resources that one guy put up https://www.linkedin.com/pulse/continuous-ios-learning-stefan-sut – silentBob Mar 12 '17 at 10:53