Help us understand the problem. What is going on with this article?

swiftでUITabBarの特定のタブをタップした時にモーダル

More than 5 years have passed since last update.

shouldSelectViewControllerで押せれたタブに紐付くViewControllerが特定のViewControllerであった場合に、タブの切り替えを行わずにモーダルを出す。

StoryBoardを使っている前提です。
SampleTabBarControllerをStoryBoard上のTabBarControllerに紐付けます。

SampleTabBarController.swift
class SampleTabBarController: UITabBarController, UITabBarControllerDelegate {

    func tabBarController(tabBarController: UITabBarController, shouldSelectViewController viewController: UIViewController) -> Bool {

        if viewController is DummyViewController {
            // DummyViewControllerはモーダルを出したい特定のタブに紐付けたViewController
            if let currentVC = self.selectedViewController{
                //表示させるモーダル
                let modalViewController: UIViewController = UIViewController()
                //わかりやすく背景を赤色に
                modalViewController.view.backgroundColor = UIColor.redColor()
                currentVC.presentViewController(modalViewController, animated: true, completion: nil)
            }
            return false
        }
        return true
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        self.delegate = self
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}

モーダルを出したいタブに紐付けられたViewController(DummyViewController)はダミー。
もっとスマートな方法は無いのかしら?

参考URL
http://blog.ryotarai.info/blog/2011/08/17/uitabbar-modal-view/

norihiko_oba
元は市役所職員で現在は原宿でiOSエンジニアをやっています
https://note.com/norihiko_oba
ubiregi
iPadを活用したSaaS型POSシステム「Ubiregi (ユビレジ)」を開発・提供しています
https://ubiregi.com
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした