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

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

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

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Swift

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

iOS 8

iOS 8(アイ・オーエス8)は、アップル社が2014年9月に発表したオペレーティングシステムです。iPhone 4sより対応しています。デザイン性の変更はなく、アプリや各種機能が強化されています。また、サードパーティ開発者のために、多くのAPIが開放されています。

Swift 2

Swift 2は、Apple社が独自に開発を行っている言語「Swift」のアップグレード版です。iOSやOS X、さらにLinuxにも対応可能です。また、throws-catchベースのエラーハンドリングが追加されています。

Q&A

解決済

1回答

3435閲覧

NavigationControllerを使用している場合の値の受け渡し

Kesth

総合スコア83

iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Swift

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

iOS 8

iOS 8(アイ・オーエス8)は、アップル社が2014年9月に発表したオペレーティングシステムです。iPhone 4sより対応しています。デザイン性の変更はなく、アプリや各種機能が強化されています。また、サードパーティ開発者のために、多くのAPIが開放されています。

Swift 2

Swift 2は、Apple社が独自に開発を行っている言語「Swift」のアップグレード版です。iOSやOS X、さらにLinuxにも対応可能です。また、throws-catchベースのエラーハンドリングが追加されています。

0グッド

0クリップ

投稿2015/09/26 06:44

編集2015/09/26 11:09

ご覧頂きありがとうございます。
首記の件ですが、NavigationControllerを使用している場合の
画面間における値の受け渡しについて質問させてください。

確認させて頂きたいのですが、NavigationControllerを使用している場合
ProtocolDelegateを使用した値の受け渡しのやり方は
できないのでしょうか?

現在、NavigationController+TableViewControllerを組み合わせた
設定画面のようなものを実装しており、そこでの値の受け渡しの
やり方で躓いています。

【追記】
現在自分が書いているソースコードを添付します。

↓遷移元です。
ProfileSettingViewControllerのProfileTextLabelのTextに遷移先から
渡された値をセットしたいです。

Swift

1import UIKit 2 3class ProfileSettingViewController: UIViewController,UITableViewDelegate,updateProfileDelegate,UITableViewDataSource,UINavigationControllerDelegate{ 4 5 //遷移先のVC 6 let profileEditView = ProfileEditViewController() 7 8 var profileTableView:UITableView! 9 10 var profileTextLabel: UILabel! 11 12 override func viewDidLoad() { 13 super.viewDidLoad() 14 15 self.view.backgroundColor = UIColor(red: 238/255, green: 238/255, blue: 238/255, alpha: 1.0) 16 17 self.navigationItem.title = "プロフィール編集" 18 19 let displayWidth: CGFloat = self.view.frame.width 20 21 profileTableView = UITableView( 22 frame: CGRect( 23 x: 0, 24 y: 135, 25 width: displayWidth, 26 height: 210 27 ),style: .Grouped 28 ) 29 30 profileTableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "profileCell") 31 profileTableView.backgroundColor = UIColor(red: 238/255, green: 238/255, blue: 238/255, alpha: 1.0) 32 profileTableView.scrollEnabled = false 33 profileTableView.dataSource = self 34 profileTableView.delegate = self 35 36 self.view.addSubview(profileTableView) 37 38 self.profileEditView.delegate = self 39 40 } 41 42 func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 43 return 1 44 } 45 46 47 /* 48 return the number of section 49 */ 50 func numberOfSectionsInTableView(tableView: UITableView) -> Int { 51 return 1 52 } 53 54 55 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 56 57 let cell = tableView.dequeueReusableCellWithIdentifier("profileCell", forIndexPath: indexPath) 58 59 if indexPath.section == 0{ 60 61 case 0: 62 cell.textLabel?.text = "プロフィール" 63 cell.textLabel?.font = UIFont(name:"Helvetica Neue", size: 14.5) 64 cell.textLabel?.textColor = UIColor(red: 62/255, green: 61/255, blue: 61/255, alpha: 0.9) 65 cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator 66 67 profileTextLabel = UILabel() 68 profileTextLabel.text = "はじめまして。よろしくお願いします。" 69 profileTextLabel.font = UIFont(name:"Helvetica Neue", size: 14.5) 70 profileTextLabel.textColor = UIColor(red: 212/255, green: 107/255, blue: 0/255, alpha: 1.0) 71 profileTextLabel.textAlignment = .Right 72 profileTextLabel.backgroundColor = UIColor.whiteColor() 73 profileTextLabel.translatesAutoresizingMaskIntoConstraints = false 74 cell.contentView.addSubview(profileTextLabel) 75 76 profileTextLabel.textAlignment = .Left 77 profileTextLabel.tag = 3 78 79 self.view.addConstraints([ 80 81 NSLayoutConstraint( 82 item: self.profileTextLabel, 83 attribute: .Left, 84 relatedBy: .Equal, 85 toItem: cell, 86 attribute: .Left, 87 multiplier: 1.0, 88 constant: 140 89 ), 90 91 NSLayoutConstraint( 92 item: self.profileTextLabel, 93 attribute: .Right, 94 relatedBy: .Equal, 95 toItem: cell, 96 attribute: .Right, 97 multiplier: 1.0, 98 constant: -27 99 ), 100 101 NSLayoutConstraint( 102 item: self.profileTextLabel, 103 attribute: .Height, 104 relatedBy: .Equal, 105 toItem: nil, 106 attribute: .Height, 107 multiplier: 1.0, 108 constant: 40 109 ), 110 111 NSLayoutConstraint( 112 item: self.profileTextLabel, 113 attribute: .Top, 114 relatedBy: .Equal, 115 toItem: cell, 116 attribute: .Top, 117 multiplier: 1.0, 118 constant: 0 119 ) 120 ]) 121 122 default: 123 print("") 124 } 125 126 } 127 return cell 128 } 129 130 131 132 func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 133 134 switch indexPath.section{ 135 case 0: 136 137 if indexPath.row == 0{ 138 self.navigationController?.pushViewController(ProfileEditViewController(), animated: true) 139 } 140 default: 141 print("") 142 } 143 } 144 145 //Delegate method 146 func updateProfile(newProfile: String){ 147 self.profileTextLabel.text = newProfile 148 self.navigationController?.popViewControllerAnimated(true) 149 } 150 151 152}

↓遷移先です。
ProfileEditViewControllerのProfileTextFieldで入力された
テキストをupdateProfileDelegateプロトコルに
セットし、遷移元に渡したいです。

Swift

1import UIKit 2 3//Delegate for updating profile 4protocol updateProfileDelegate{ 5 func updateProfile(newProfile:String) 6} 7 8class ProfileEditViewController: UIViewController,UINavigationControllerDelegate{ 9 10 var refleshButton: UIButton! 11 12 var profileTextField: UITextField! 13 14 //Delegate for updating profile 15 var delegate: updateProfileDelegate! = nil 16 17 override func viewDidLoad() { 18 super.viewDidLoad() 19 20 self.view.backgroundColor = UIColor(red: 238/255, green: 238/255, blue: 238/255, alpha: 1.0) 21 22 self.navigationItem.title = "プロフィール編集" 23 24 refleshButton = UIButton() 25 refleshButton.setTitle("更新", forState: .Normal) 26 refleshButton.addTarget(self, action: "refleshProfile:", forControlEvents: .TouchUpInside) 27 refleshButton.layer.masksToBounds = true 28 refleshButton.layer.cornerRadius = 4.0 29 refleshButton.tintColor = UIColor(red: 238/255, green: 238/255, blue: 238/255, alpha: 1.0) 30 refleshButton.backgroundColor = UIColor(red: 242/255, green: 147/255, blue: 9/255, alpha: 1.0) 31 refleshButton.translatesAutoresizingMaskIntoConstraints = false 32 self.view.addSubview(refleshButton) 33 34 profileTextField = UITextField() 35 profileTextField.contentVerticalAlignment = UIControlContentVerticalAlignment.Top 36 profileTextField.textAlignment = .Left 37 profileTextField.leftView = paddingLeft 38 profileTextField.leftViewMode = UITextFieldViewMode.Always//for left padding 39 profileTextField.translatesAutoresizingMaskIntoConstraints = false 40 self.view.addSubview(profileTextField) 41 42 //Add constraints 43 setupConstraints() 44 } 45 46 //Delegate for updating profile 47 /******* 48 ここでエラーが発生します(エラー内容は添付のスクショをご覧ください) 49 *******/ 50 func refleshProfile(sender: AnyObject){ 51 self.delegate.updateProfile(profileTextField.text!) 52 } 53 54 55 func setupConstraints(){ 56 //文字数の関係で省略します 57 } 58 59 60}

エラー内容
イメージ説明

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

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

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

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

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

TakeOne

2015/09/26 08:10 編集

いろいろ言葉があいまいで状況がよくわからないので回答しづらいです。 「TableView」とか「EditView」とか書いているけどTableViewControllerやViewControllerのことなのかな?とか、「エラーを吐く」と書いているけどどんなエラーメッセージなのかな?とか。少なくともNavigationController配下のViewController間のpush画面遷移の話なら、参考にしたページのようにやれば普通に受け渡しできるはずです。ただ、tableViewのセルに結果を表示したいなら、そのあたりは参考ページ以外に考えなければならないところだと思います。いずれにしてもソースコードを貼ってエラーメッセージ内容を明確にしてもらえば何かわかると思います。
Kesth

2015/09/26 11:09

情報が曖昧&不足しており、申し訳ありません。 たった今、ソースコードを貼りました。 もしご不明な点などありましたらお知らせください。 よろしくお願いします。
guest

回答1

0

ベストアンサー

let profileEditView = ProfileEditViewController()
で、ProfileEditViewControllerを生成し、
self.profileEditView.delegate = self
で、ちゃんとdelegateを設定しているのに、
画面遷移は、
self.navigationController?.pushViewController(ProfileEditViewController(), animated: true)
となっていて、事前に生成したProfileEditViewControllerを使用するのではなく、ここで新しいProfileEditViewControllerを生成して、delegateを設定しないまま画面遷移しているので、最終的にdelegateがなくてエラーになっているのだと思います。
質問を編集する前に提示していた
http://tech.eversense.co.jp/153
と同じようにするなら
self.navigationController?.pushViewController(self.profileEditView, animated: true)
とすればよいと思います。

毎回新しいProfileEditViewControllerを生成して画面遷移したいなら、事前生成はやめて、画面遷移直前に生成したProfileEditViewControllerにdelegateを設定してから遷移すればよいと思います。

投稿2015/09/26 12:10

TakeOne

総合スコア6299

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

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

Kesth

2015/09/26 16:14

大変わかりやすい解説をありがとうございます。 おかげさまで無事実装できました。 ありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問