8
11

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

swift3.0でtabBar間でイベントを渡す

Last updated at Posted at 2017-03-25

タブをタップしてイベントを拾う場合ではなく、View上のコントローラをタップした時に自動的にタブを移動してかつ移動した先の画面でイベントを発火させたい場合のメモです。

■環境

  • swift3.0
  • tabBarController
  • tableView

■イメージ
ず2.png

たぶ1ViewController.swift
// セルがタップされた時
func tableView(_ table:UITableView, didSelectRowAt indexPath:IndexPath) {
    // タブを切り替える
    self.tabBarController?.selectedIndex = 1
}
tabBarController.swift
protocol TabBarDelegate {    
    func didSelectTab(tabBarController: UITabBarController)
}
class baseTabBarController: UITabBarController, UITabBarControllerDelegate {
    @IBOutlet weak var myTabBar: UITabBar!
    override var selectedIndex: Int{
        // タブ切り替え時に処理を行うため
        didSet {
            self.delegate?.tabBarController?(self, didSelect: self.viewControllers![selectedIndex])
        }
    }    
    override func viewDidLoad() {
        super.viewDidLoad()  
        self.delegate = self
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
        if viewController is TabBarDelegate {
            let v = viewController as! TabBarDelegate
            v.didSelectTab(tabBarController: self)
        }
    }
}
たぶ2ViewController.swift
    func didSelectTab(tabBarController: UITabBarController) {
        // 処理
    }
8
11
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
11

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?