質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Q&A

解決済

2回答

719閲覧

NavigationBarの戻るボタンの連打防止

yamasa

総合スコア23

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

0グッド

2クリップ

投稿2017/07/26 13:34

編集2017/07/27 14:57

###前提・実現したいこと
NavigationBar <Backボタンの連打防止

###発生している問題・エラーメッセージ
A->B->Cへ画面遷移した後
C画面にて表示されているNavigationBarの戻るボタン[< B]を連打すると
A画面まで戻ってしまいます。

NavigationBarのボタンに対して
isEnableの設定など出来るのでしょうか。

何か有効な手段を教えて頂けますでしょうか

###試したこと

lang

1import UIKit 2 3class FirstViewController: UIViewController { 4 5 override func viewDidLoad() { 6 super.viewDidLoad() 7 8 self.view.backgroundColor = UIColor.red 9 10 // ボタンを生成する. 11 let myButton = UIButton(frame: CGRect(x:0,y:0,width:100,height:100)) 12 myButton.backgroundColor = UIColor.orange 13 myButton.setTitle("ボタン", for: .normal) 14 myButton.layer.position = CGPoint(x: self.view.bounds.width/2, y:200) 15 myButton.addTarget(self, action: #selector(FirstViewController.onClickMyButton(sender:)), for: .touchUpInside) 16 17 // ボタンをViewに追加する. 18 self.view.addSubview(myButton); 19 20 // NavigationBarの表示する. 21 self.navigationController?.setNavigationBarHidden(false, animated: false) 22 self.navigationItem.title = "A" 23 } 24 25 /* 26 ボタンイベント 27 */ 28 internal func onClickMyButton(sender: UIButton){ 29 print("onClickMyButton:") 30 31 let secondViewController = SecondViewController() 32 self.navigationController?.pushViewController(secondViewController, animated: true) 33 } 34 35 override func didReceiveMemoryWarning() { 36 super.didReceiveMemoryWarning() 37 // Dispose of any resources that can be recreated. 38 } 39} 40

lang

1import UIKit 2 3class SecondViewController: UIViewController { 4 5 override func viewDidLoad() { 6 super.viewDidLoad() 7 8 self.title = "B" 9 self.view.backgroundColor = UIColor.yellow 10 11 // ボタンを生成する. 12 let myButton = UIButton(frame: CGRect(x:0,y:0,width:100,height:100)) 13 myButton.backgroundColor = UIColor.orange 14 myButton.setTitle("ボタン", for: .normal) 15 myButton.layer.position = CGPoint(x: self.view.bounds.width/2, y:200) 16 myButton.addTarget(self, action: #selector(SecondViewController.onClickMyButton(sender:)), for: .touchUpInside) 17 18 // ボタンをViewに追加する. 19 self.view.addSubview(myButton); 20 } 21 22 23 override func didReceiveMemoryWarning() { 24 super.didReceiveMemoryWarning() 25 // Dispose of any resources that can be recreated. 26 } 27 28 /* 29 ボタンイベント 30 */ 31 internal func onClickMyButton(sender: UIButton){ 32 print("onClickMyButton:") 33 34 let thirdViewController = ThirdViewController() 35 self.navigationController?.pushViewController(thirdViewController, animated: true) 36 } 37 38}

lang

1import UIKit 2 3class ThirdViewController: UIViewController { 4 5 override func viewDidLoad() { 6 super.viewDidLoad() 7 self.title = "C" 8 self.view.backgroundColor = UIColor.blue 9 } 10 11 override func didReceiveMemoryWarning() { 12 super.didReceiveMemoryWarning() 13 // Dispose of any resources that can be recreated. 14 } 15 16}

###補足情報(言語/FW/ツール等のバージョンなど)
XCode8.3.3
Swift3

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

swift

1 override func viewWillAppear(_ animated: Bool){ 2 super.viewWillAppear(true) 3 print("カウント\(self.navigationController?.viewControllers.count)") 4 if(self.navigationController?.viewControllers.count == 2){ 5 self.navigationItem.hidesBackButton = true 6 } 7 8 } 9

こんな感じで画面に来た際にナビゲーションのカウント数えて戻るボタンをなくすのはどうでしょうか?

sampleでgithubに記述追加したのあげて見ました。参考にもしよかったらと思い乗せておきます。
https://github.com/sachiko-kame/swift.sample11/commits/master

と思ったのですが、ナビゲーションのカウントなしに、

swift

1 override func viewWillAppear(_ animated: Bool){ 2 super.viewWillAppear(true) 3 self.navigationItem.hidesBackButton = true 4 } 5

で消せると思いました。viewに来たらの意味なので。

投稿2017/07/29 12:30

編集2017/07/29 17:03
sachiko-kame

総合スコア334

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

yamasa

2017/07/30 02:00

self.navigationItem.hidesBackButton = true で隠せるんですね。知らなかった。。。。 ちなみに。 C画面のBackボタンを押した直後に非表示にしたいのですが 自己解決方に記載しておきます。 回答ありがとうございました!
sachiko-kame

2017/07/30 05:01

解決してよかったです(✿´ ꒳ ` )
guest

0

自己解決

ThirdViewControllerの
viewWillDisappearで
self.navigationItem.hidesBackButton = true
を追加しました。

lang

1import UIKit 2 3class ThirdViewController: UIViewController { 4 5 override func viewDidLoad() { 6 super.viewDidLoad() 7 self.title = "C" 8 self.view.backgroundColor = UIColor.blue 9 } 10 11 override func didReceiveMemoryWarning() { 12 super.didReceiveMemoryWarning() 13 // Dispose of any resources that can be recreated. 14 } 15 16 override func viewWillDisappear(_ animated: Bool) { 17 super.viewWillDisappear(animated) 18 self.navigationItem.hidesBackButton = true 19 } 20} 21

投稿2017/07/30 02:02

yamasa

総合スコア23

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問