Set Action for UITabBarItem

纵然是瞬间 提交于 2019-12-12 02:15:28

问题


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.


回答1:


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
    }
        }
    }



回答2:


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
    }
}



回答3:


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.



来源:https://stackoverflow.com/questions/42681013/set-action-for-uitabbaritem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!