ご覧頂きありがとうございます。
首記の件ですが、NavigationControllerを使用している場合の
画面間における値の受け渡しについて質問させてください。
確認させて頂きたいのですが、NavigationControllerを使用している場合
ProtocolやDelegateを使用した値の受け渡しのやり方は
できないのでしょうか?
現在、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}
回答1件
あなたの回答
tips
プレビュー