UITabBarControllerDelegateが効かない
やりたいこと
StoryboardでiOSアプリを開発していて、TabBarを使っている。 現在すでに選択されているTabをタップしたら、tableViewを一番上までスクロールしたい。TwitterとかInstagramだとよくあるやつ。
UITabBarControllerDelegateをAppDelegateに書いてみたけど、効かない。
解決策
- 「Add New File」をして、TabBarController.swift(UITabBarController)を加える。
- なぜか「import Cocoa」なソースコードが出てきてエラーになるので、手で以下のように修正。
import UIKit class TabBarController: UITabBarController, UITabBarControllerDelegate { override func viewDidLoad() { super.viewDidLoad() self.delegate = self } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } func tabBarController(tabBarController: UITabBarController, didSelectViewController viewController: UIViewController) { if (tabBarController.selectedIndex == 0) { var navigationController : UINavigationController = viewController as UINavigationController var listViewController : ListViewController = navigationController.viewControllers[0] as ListViewController listViewController.tableView.setContentOffset(CGPointMake(0, 0 - listViewController.tableView.contentInset.top), animated: true) } } }