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

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

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

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

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

Q&A

2回答

2860閲覧

[Swift]画面遷移と同時にNavigationBarのtitleを変更したい

niship

総合スコア37

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

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

0グッド

0クリップ

投稿2017/05/31 12:03

ご覧いただき、有難うございます。
DropDownMenuのライブラリを使用しているのですが、
メニューの「Month」セルをタップすると、画面遷移と同時にナビゲーションバータのイトルも「Month」に変わるのですが、そこから「Day」セルをタップしても、画面遷移とともにナビゲーションバーのタイトルが変わりません。

また、「Month」のViewControllerにはSegueを繋いでいないので、NavigationControllerを継承していません。やや無理やりですが、NavigationControllerを生成しております。AppDelegateでNavigationControllerを生成しようとしましたが、うまくいきませんでした。

下記URLを参考にしました。
【Swift】BTNavigationDropdownMenu使ってみた【簡単にドロップダウンメニューが作れる!】

タップしたメニューに適したタイトル(「Day」なら「Day」「Month」なら「Month」)に変更するためにはどのようにコードを訂正すればよろしいでしょうか。ご教示お願いいたします。

// ViewController.swift let items = ["Daily", "Month"] var menuView: BTNavigationDropdownMenu! override func viewDidLoad() { super.viewDidLoad() var menuView = BTNavigationDropdownMenu(navigationController: self.navigationController, containerView: self.navigationController!.view, title: "Dropdown Menu", items: items as [AnyObject]) menuView = BTNavigationDropdownMenu(title: items[0], items: items as [AnyObject]) self.navigationController?.navigationBar.isTranslucent = false self.navigationController?.navigationBar.barTintColor = UIColor(red: 0.0/255.0, green:180/255.0, blue:220/255.0, alpha: 1.0) self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white] self.navigationItem.titleView = menuView menuView.didSelectItemAtIndexHandler = {[weak self] (indexPath: Int) -> () in print("Did select item at index: \(indexPath)") if indexPath == 1 { let monthViewController = self?.storyboard?.instantiateViewController(withIdentifier: "Month") let navigationController = UINavigationController(rootViewController: monthViewController!) self?.present(navigationController, animated: false, completion:nil) } } }
// MonthViewController.swift let items = ["Daily", "Month"] var menuView: BTNavigationDropdownMenu! override func viewDidLoad() { super.viewDidLoad() var menuView = BTNavigationDropdownMenu(navigationController: self.navigationController, containerView: self.navigationController!.view, title: "Dropdown Menu", items: items as [AnyObject]) menuView = BTNavigationDropdownMenu(title: items[1], items: items as [AnyObject]) self.navigationController?.navigationBar.isTranslucent = false self.navigationController?.navigationBar.barTintColor = UIColor(red: 0.0/255.0, green:180/255.0, blue:220/255.0, alpha: 1.0) self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white] self.navigationItem.titleView = menuView menuView.didSelectItemAtIndexHandler = {[weak self] (indexPath: Int) -> () in print("Did select item at index: \(indexPath)") if indexPath == 0 { self?.dismiss(animated: true, completion: nil) } } }

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

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

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

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

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

fuzzball

2017/06/01 00:28 編集

(deleted)
guest

回答2

0

検討違いだったらごめんなさい。そのライブラリをつかったことはないのですが、ナビゲーションバーのことから、なんか気になるところがあったので書きます。

var menuView = BTNavigationDropdownMenu(navigationController: self.navigationController, containerView: self.navigationController!.view, title: "Dropdown Menu", items: items as [AnyObject])

このコードですが、最後の

self.navigationItem.titleView = menuView

に当てはめていると思います。ちなみにこの設定はナビゲーションバーのタイトルにviewを入れる設定なのでナビゲーションにtitleをそのまま入れるのとは少し異なります。
なのでそこの挙動が変になっているということは考えられないでしょうか?

ちなみにそのライブラリでも

self.title = "設定"

のようにそのファイルのクラスのタイトルを設定できるでしょうか、そしたらその方がもしかしたらうまく行くのではと考えて見ました。

ちなみに今回の感じの少しでも参考になるかもしれないとおもったのでgithubのURL乗せておきます。これは設置の簡易バージョンで作って見たものです。

投稿2017/06/03 08:17

sachiko-kame

総合スコア334

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

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

0

「Day」セルをタップしても、画面遷移とともにナビゲーションバーのタイトルが変わりません

そもそも遷移していませんが。

投稿2017/06/01 00:28

編集2017/06/01 00:28
fuzzball

総合スコア16731

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

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

niship

2017/06/01 00:43 編集

ご回答有難うございます。 下記のコードで遷移先のViewにNavigationControllerを生成し、 遷移できているものだと思っておりましたが、その解釈が間違っていたのでしょうか。 let monthViewController = self?.storyboard?.instantiateViewController(withIdentifier: "Month") let navigationController = UINavigationController(rootViewController: monthViewController!) self?.present(navigationController, animated: false, completion:nil) //前の画面へ戻る self?.dismiss(animated: true, completion: nil)
fuzzball

2017/06/01 00:41

それはMonthへの遷移ですよね?
niship

2017/06/01 00:48

失礼しました。今追記しましたが、下記のコードは、ViewControllerに遷移(前の画面へ戻る)しているのではないのでしょうか。 self?.dismiss(animated: true, completion: nil) 実際、シミュレータではViewControllerの画面に移り変わっているのですが、遷移とはまた解釈が違うのでしょうか。
fuzzball

2017/06/01 00:51

最初に表示されるViewControllerがDay(Daily)ということですか? 最初のViewControllerから、Day(Daily)とMonthのそれぞれに遷移するのかと思っていました。
niship

2017/06/01 00:55

わかりづらくて申し訳有りません。最初の画面(ViewController)がDayです。なのでDayをタップするとViewController、MonthをクリックするとMonthViewControllerへ遷移させたいと思っております。Segueを繋いでおりませんので、コードにて遷移をさせているのですが、MonthViewControllerからDayをタップした時に画面は遷移するのですが、NavigationBarのタイトル表示がMonthから変更されないのです。
fuzzball

2017/06/01 01:10

UINavigationControllerの仕組みや使い方は理解されているのでしょうか? 一から作り直さないといけないレベルのコードで、ちょっと面倒見きれません。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問