Warning: Use of undefined constant user_level - assumed 'user_level' (this will throw an Error in a future version of PHP) in /home/users/1/juny/web/hidef/wp-content/plugins/ultimate-google-analytics/ultimate_ga.php on line 524

Warning: Use of undefined constant user_level - assumed 'user_level' (this will throw an Error in a future version of PHP) in /home/users/1/juny/web/hidef/wp-content/plugins/ultimate-google-analytics/ultimate_ga.php on line 524

[Swift] UITabBarControllerでタブ選択時のイベントを取得する

公開日: : 最終更新日:2015/08/12 iPhone App 開発, Swift


Warning: Use of undefined constant user_level - assumed 'user_level' (this will throw an Error in a future version of PHP) in /home/users/1/juny/web/hidef/wp-content/plugins/ultimate-google-analytics/ultimate_ga.php on line 524

UITabBarController

タブを使った画面を作成する場合はUITabBarControllerを使用します。
今回はそのタブ選択時のイベントを取得してみます。

新規プロジェクトから「Tabbed Application」テンプレートを選択します。
スクリーンショット 2014-12-04 12.46.18
デフォルトでFirstViewControllerとSecondViewControllerがあります。
こちらの2つのViewControllerは、UITabBarControllerのviewControllersに内包されている状態です。
スクリーンショット 2014-12-04 13.05.50

デリゲートの作成

手順としてはUITabControllerのサブクラスを作成し、そのクラスからタブ選択時に呼び出されるDelegateを作成し、各ViewControllerに委譲します。

まずはDelegate。
以下の内容で新規ファイルを作成します。ファイル名はTabBarDelegate.swiftとしました。

import Foundation

@objc protocol TabBarDelegate {

    func didSelectTab(tabBarController: TabBarController)
}

UITabBarControllerのサブクラス

次はUITabBarControllerのサブクラスを作成します。名前はTabBarController.swiftです。
スクリーンショット 2014-12-04 13.03.36

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 viewController is TabBarDelegate {
            let v = viewController as! TabBarDelegate
            v.didSelectTab(self)
        }
    }
}

7行目で自分自身にデリゲートを設定します。
14行目はタブ選択時のデリゲート(UITabBarController)です。
どのViewControllerのタブが押されたのか引数で渡されてくるので、15行目でTabBarDelegateを実装しているか判断します。 17行目でViewControllerのデリゲートを呼び出します。(TabBarDelegate)

StoryBoardの設定

TabControllerを選択し、「Custom Class」のClassに先ほど作成した「TabBarController」を入力します。
スクリーンショット 2014-12-04 13.05.08

各子画面の設定

最後に各ViewControllerで以下のようにDelegateを追加します。FirstViewController.swiftの内容です。
(SecondViewControllerjの内容は省きます。)

import UIKit

class FirstViewController: UIViewController, TabBarDelegate {

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

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

    func didSelectTab(tabBarController: TabBarController) {
        println("first!")
    }
}

3行目でTabBarDelegateを使うと宣言します。
13行目でTabBarDelegateのdidSelectTabが呼ばれます。

結果

iOS Simulator Screen Shot 2014.12.04 17.03.36
スクリーンショット 2014-12-04 17.04.37

まとめ

今回はタグ選択時のイベントを、個々のViewControllerでデリゲートとして受け取りました。
単純にViewController読み込み時に処理をしたい場合はviewWillAppearでもよいと思います。
ただ、viewWillAppearが発生するのはタブ切り替え時のみです。(同じタブを押下してもviewWillAppearは呼ばれません。)

ad

関連記事

[Objective-C] 小数点を丸める!(数値変換)

小数点を含む数値 Objective-Cに限らず、小数点を含む数値を計算する場合はdoubleやf

記事を読む

no image

爆走中!

挫折街道爆走中です! が! 迷走中でもあるわけです、はい。 そんな中で、迷走するに至った訳

記事を読む

[iPhone App] WorkManagerの不具合について

バージョン1.2.2の不具合 11/25日現在、iPhoneアプリの「WorkManger」バージ

記事を読む

[Objective-C] 文字列操作の便利Tips

Tipsいろいろ 先日の投稿 共通関数をまとめたクラスを作ろう!の流れで、文字列を操作するときに便

記事を読む

[Swift] プロパティリスト(plist)を使ってCoreDataで一括登録する

プロパティリストを使う 初期データやあらかじめ用意したデータを一括登録したい場合、プロパティリスト

記事を読む

[Objective-C] iPhoneアプリからのメーラー起動

今回はiPhoneアプリからメーラーを起動する処理を紹介します。 メーラー起動のコントローラー

記事を読む

[Objective-C] バージョン更新時に変更内容をアラートで出力する

アプリの更新内容を表示したい! バージョン更新時に変更内容を表示させたいのは、ユーザーがいちいちA

記事を読む

TabBarControllerとNavigationControllerのボタン追加

TabBarとNavigationを同時に使っているViewControllerの場合、naviga

記事を読む

[Swift] ビルド時のエラー&iOS8でNSBigDecimalのバグ

ビルド時のエラー Swiftプロジェクトを実行中に以下のエラーが出ました。 "__TFSs15_

記事を読む

[Objective-C] no architectures to compile for … のコンパイルエラーについて

起動したプロジェクトでコンパイルエラーが出ました。 no architectures to com

記事を読む

ad

Comment


  1. Warning: Use of undefined constant user_level - assumed 'user_level' (this will throw an Error in a future version of PHP) in /home/users/1/juny/web/hidef/wp-content/plugins/ultimate-google-analytics/ultimate_ga.php on line 524
    めばえ より:

    Warning: Use of undefined constant user_level - assumed 'user_level' (this will throw an Error in a future version of PHP) in /home/users/1/juny/web/hidef/wp-content/plugins/ultimate-google-analytics/ultimate_ga.php on line 524

    はじめまして。タブビューアプリを作成するにあたってGoogle検索にてこちらの記事を見つけました。
    早速記載されている通りの記述を行ったのですが、どうもデリゲートしているはずなのに「viewController」がデリゲートではないと判断されてしまいます。
    箇所で言うと「if viewController is TabBarDelegate {}」が実行されていない状態です。
    storyboardの親TabControllerではカスタムしたクラスを選択しているので反応はしています。
    この点で悩んでいますので、なにかご教授願えばと思います。よろしくお願いいたします。


    • Warning: Use of undefined constant user_level - assumed 'user_level' (this will throw an Error in a future version of PHP) in /home/users/1/juny/web/hidef/wp-content/plugins/ultimate-google-analytics/ultimate_ga.php on line 524
      north より:

      Warning: Use of undefined constant user_level - assumed 'user_level' (this will throw an Error in a future version of PHP) in /home/users/1/juny/web/hidef/wp-content/plugins/ultimate-google-analytics/ultimate_ga.php on line 524

      コメントありがとうございます。お返事遅くなり申し訳ございません。
      再度作成・実行してみましたが、上記の現象は再現しませんでした。
      FirstViewController、SecondViewControllerにTabBarDelegateが実装されていますでしょうか?
      (ソースコードの3行目にあたる部分です)
      実装されていれば、インスタンスはTabBarDelegateだと認識し、if文が通ると思われます。
      ご確認宜しくお願い致します。


  2. Warning: Use of undefined constant user_level - assumed 'user_level' (this will throw an Error in a future version of PHP) in /home/users/1/juny/web/hidef/wp-content/plugins/ultimate-google-analytics/ultimate_ga.php on line 524
    チワワ より:

    Warning: Use of undefined constant user_level - assumed 'user_level' (this will throw an Error in a future version of PHP) in /home/users/1/juny/web/hidef/wp-content/plugins/ultimate-google-analytics/ultimate_ga.php on line 524

    swift3で作成しています。

    タブ選択時の処理を追加したくてこちらを参考にさせていただいています。
    上記ソースを参照し作成したのですが、
    (一部エラーになったため修正しています。)
    タブを選択しても何もコンソールに表示されません。

    修正した箇所はTabBarController.swiftの
    17行目の
    v.didSelectTab(self)

    v.didSelectTab(tabBarController: self)に
    変更しました。
    またFirstViewControllerの
    println(“first!”)をprint(“first!”)に

    何故に出力されないのでしょうか?


  3. Warning: Use of undefined constant user_level - assumed 'user_level' (this will throw an Error in a future version of PHP) in /home/users/1/juny/web/hidef/wp-content/plugins/ultimate-google-analytics/ultimate_ga.php on line 524
    UITabBarControllerを利用したてアクティブなタブが再度タップされたとき - swift3 uitabbarcontroller - 質問と回答 より:

    Warning: Use of undefined constant user_level - assumed 'user_level' (this will throw an Error in a future version of PHP) in /home/users/1/juny/web/hidef/wp-content/plugins/ultimate-google-analytics/ultimate_ga.php on line 524

    […] こちらのサイトが参考になるかと思います。 […]

めばえ へ返信する コメントをキャンセル

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

ad

[PHP] curl転送してみる(googleに)

curlでgoogle画像検索APIにアクセスしてみます。 cu

[PHP] PDOでMySQLの接続確認をする

PDO PHPでMySQLに接続する際には、mysql_connec

[PHP] ランダムな英数字を生成する

便利系メソッド 今回はPHPでランダムな英数字を作成してみます。

[Swift] プロパティリスト(plist)の値を取得

plistからデータを取得してみます。 こちらのエントリーも参考にし

[Swift] Asset Catalogについて

XCode5から追加されたAsset Catalog。 いままで標準

→もっと見る

  • 1978年の七夕生まれ。 25才でweb業界の門を叩き、28才でフリーランスに。 現在は、フリーランスでマークアップ中心に、wordpressのカスタマイズやデザインをしております。 また、iPhoneアプリの開発もしております。

Warning: Use of undefined constant user_level - assumed 'user_level' (this will throw an Error in a future version of PHP) in /home/users/1/juny/web/hidef/wp-content/plugins/ultimate-google-analytics/ultimate_ga.php on line 524
PAGE TOP ↑

Warning: Use of undefined constant user_level - assumed 'user_level' (this will throw an Error in a future version of PHP) in /home/users/1/juny/web/hidef/wp-content/plugins/ultimate-google-analytics/ultimate_ga.php on line 524