[Swift] UINavigationControllerで戻るイベントを取得する
公開日:
:
最終更新日: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
UINavigationController
UINavigationControllerを使用して画面を構成したときに、ナビゲーションバーに戻るボタンが自動的に挿入されます。

遷移後の画面から前画面に戻るときに、なんらかの処理をしたい場合は以下の方法で取得できます。
DetailViewController.swift
MasterViewController.swiftが元画面、DetailViewController.swiftが遷移後の画面だとします。
遷移後のDetailViewController.swiftに以下のコードを追加します。
override func viewWillDisappear(animated: Bool) {
let viewControllers = self.navigationController?.viewControllers!
if indexOfArray(viewControllers!, searchObject: self) == nil {
// 戻るボタンが押された処理
println("back!")
}
super.viewWillDisappear(animated)
}
func indexOfArray(array:[AnyObject], searchObject: AnyObject)-> Int? {
for (index, value) in enumerate(array) {
if value as! UIViewController == searchObject as! UIViewController {
return index
}
}
return nil
}
viewWillDisappearは表示している画面が別画面に遷移する直前に呼ばれます。
2行目でUINavigationControllerに内包されているUIViewControllerの一覧を取得。
このviewControllersには遷移する毎にUIViewControllerが追加されていきます。
viewWillDisappearが呼ばれた時点で自コントローラーは一覧からなくなっている状態なので(オブジェクトはまだ存在する)、3行目で存在チェック。
viewControllersは[AnyObject]!なのでオブジェクト同士を比較するための処理が必要です。
10行目で新規にindexOfArrayという関数を用意しました。内容は、UIViewController同士を比較して同じViewControllerであればそのIndexを返し、存在しない場合はnilが返却されます。
Objective-Cの場合
Objective-Cの場合もSwiftと同様にviewWillDisappearに記述します。
-(void)viewWillDisappear:(BOOL)animated
{
if ([self.navigationController.viewControllers indexOfObject:self] == NSNotFound)
{
// 戻るボタンが押された処理
NSLog(@"back!");
}
[super viewWillDisappear:animated];
}
ad
関連記事
-
-
App開発をはじめてみました。
はじめてみたはいいものの、右も左もわからず、書籍を読みあさり、ネットを徘徊し、いろんなことを調べ上げ
-
-
[Swift] Swiftでいろんなfor文まとめてみた
いろんなfor文 プログラム作成で必ず出てくるfor文を自分なりにまとめてみました。 Swift
-
-
[Swift] スクリーンサイズを取得する
スクリーンサイズを取得 iPhoneの幅と高さを取得する関数を紹介します。 iOS8から画面の傾
-
-
[Swift] 定数クラスをつくってみる
Objective-cの場合 今回は共通にする定数をまとめるクラスを作ってみます。 まずはO
-
-
[Objective-C] 小数点を丸める!(数値変換)
小数点を含む数値 Objective-Cに限らず、小数点を含む数値を計算する場合はdoubleやf
-
-
[Swift] 関数型プログラミング
関数型プログラミングについて、ストーリー仕立ての面白い記事がありましたので紹介します。 IQ1
-
-
[Objective-C] no architectures to compile for … のコンパイルエラーについて
起動したプロジェクトでコンパイルエラーが出ました。 no architectures to com
-
-
NSDateを5分刻み・15分刻みで丸める
NsDateを初期値としてUIDatePicker等に設定する場合、日付の丸め処理が必要になるのでメ
-
-
[Swift] CoreDataのテーブルからデータ件数を取得する
件数を取得する fetchResultsControllerなどを使わず、直接テーブルのデータ件数
Comment
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
na より:
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
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
コメントありがとうございます。お返事遅くなり申し訳ございません。
スワイプで戻る場合でのキャンセルの事象がこちらでも確認できました。
対処法ですが、viewWillDisappearでviewControllerのnil判定をしています。
ここで、フラグか何か設定しておいてviewDidDisappearで判断するのはどうでしょう。
ご確認宜しくお願い致します。