プロフィールページからプロフィール編集ページへモーダル表示をしています。プロフィールを編集してモーダルを閉じた時に即座に変更点がプロフィールに反映されないためプロトコルを用いて変更をすぐに反映させようとしていますが、できません。
EditProfileViewController.swift
import UIKit import NCMB import NYXImagesKit import PKHUD protocol ProfileEditDelegate { func didEditProfile(profileText: String) } class EditProfileViewController: UIViewController, UITextViewDelegate, UITextFieldDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate { @IBOutlet var mainImageView: UIImageView! @IBOutlet var subImageCollectionView: UICollectionView! @IBOutlet var selectMainImageButton: UIButton! @IBOutlet var displayNameTextField: UITextField! @IBOutlet var searchIdTextfield: UITextField! @IBOutlet var profileTextView: UITextView! @IBOutlet var logOutButton: UIButton! override func viewDidLoad() { super.viewDidLoad() if let user = NCMBUser.current(){ displayNameTextField.text = user.object(forKey: "userName") as? String searchIdTextfield.text = user.object(forKey: "searchId") as? String profileTextView.text = user.object(forKey: "profileSentence") as? String let file = NCMBFile.file(withName: NCMBUser.current()?.objectId, data: nil) as! NCMBFile file.getDataInBackground { (data, error) in if error != nil{ print(error) }else{ if data != nil { let image = UIImage(data: data!) self.mainImageView.image = image } } } }else{ } } var editDelegate: ProfileEditDelegate? @IBAction func saveProfile(){ let user = NCMBUser.current() user?.setObject(self.displayNameTextField.text, forKey: "userName") user?.setObject(self.searchIdTextfield.text, forKey: "searchId") user?.setObject(self.profileTextView.text, forKey: "profileSentence") editDelegate?.didEditProfile(profileText: profileTextView.text) print("ああああああああああああああああああ") user?.saveInBackground({ (error) in if error != nil { print(error) }else{ print("いいいいいいいいいいいいいいいいいい") self.dismiss(animated: true, completion: nil) } }) } @IBAction func cancel(){ self.dismiss(animated: true, completion: nil) } func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) { let selectedImage = info[UIImagePickerController.InfoKey.originalImage] as! UIImage let resizedImage = selectedImage.scale(byFactor: 0.1) picker.dismiss(animated: true, completion: nil) let data = resizedImage!.pngData() let file = NCMBFile.file(withName: NCMBUser.current()?.objectId, data: data) as! NCMBFile file.saveInBackground({ (error) in if error != nil{ print(error) }else{ self.mainImageView.image = resizedImage } }) { (progress) in print(progress) } } }
UserPageViewController.swift
import
1import NCMB 2import PKHUD 3 4class UserPageViewController: UIViewController, ProfileEditDelegate{ 5 func didEditProfile(profileText: String) { 6 print("ーーーーーーー呼ばれてる") 7 profileTextView.text = profileText 8 print("ーーーーーーー呼ばれてる") 9 } 10 11 12 13 @IBOutlet var mainImageView: UIImageView! 14 15 @IBOutlet var subImageCollectionView: UICollectionView! 16 @IBOutlet var nameLabel: UILabel! 17 @IBOutlet var followerButton: UIButton! 18 @IBOutlet var followingButton: UIButton! 19 @IBOutlet var profileTextView: UITextView! 20 @IBOutlet var goToEdit: UIButton! 21 22 23 24 25 26 override func viewDidLoad() { 27 super.viewDidLoad() 28 29 30 31 if let user = NCMBUser.current(){ 32 33 HUD.flash(.progress, delay: 0.1){ 34 finished in 35 DispatchQueue.main.async { 36 self.nameLabel.text = user.object(forKey: "userName") as? String 37 self.profileTextView.text = user.object(forKey: "profileSentence") as? String 38 self.navigationItem.title = user.object(forKey: "searchId") as? String 39 let file = NCMBFile.file(withName: NCMBUser.current()?.objectId, data: nil) as! NCMBFile 40 41 file.getDataInBackground { (data, error) in 42 if error != nil { 43 print(error) 44 }else{ 45 if data != nil{ 46 let image = UIImage(data: data!) 47 self.mainImageView.image = image 48 } 49 50 } 51 } 52 } 53 } 54 55 56 }else{ 57 let storyboard = UIStoryboard(name: "SignIn", bundle: Bundle.main) 58 let rootViewController = storyboard.instantiateViewController(withIdentifier: "RootNavigationController") 59 UIApplication.shared.keyWindow?.rootViewController = rootViewController 60 61 let ud = UserDefaults.standard 62 ud.set(false, forKey: "isLogin") 63 ud.synchronize() 64 } 65 } 66 67 override func viewWillAppear(_ animated: Bool) { 68 if let user = NCMBUser.current(){ 69 70 nameLabel.text = user.object(forKey: "userName") as? String 71 profileTextView.text = user.object(forKey: "profileSentence") as? String 72 navigationItem.title = user.object(forKey: "searchId") as? String 73 74 let file = NCMBFile.file(withName: NCMBUser.current()?.objectId, data: nil) as! NCMBFile 75 76 file.getDataInBackground { (data, error) in 77 if error != nil { 78 print(error) 79 }else{ 80 if data != nil{ 81 let image = UIImage(data: data!) 82 self.mainImageView.image = image 83 } 84 85 } 86 } 87 88 print(profileTextView) 89 90 91 }else{ 92 let storyboard = UIStoryboard.init(name: "SignIn", bundle: Bundle.main) 93 94 let rootViewController = storyboard.instantiateViewController(withIdentifier: "RootNavigationController") 95 UIApplication.shared.keyWindow?.rootViewController = rootViewController 96 97 let ud = UserDefaults.standard 98 ud.set(false, forKey: "isLogin") 99 ud.synchronize() 100 101 } 102 103 104 105 106 107 } 108 109 110 override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 111 if segue.identifier == "fromUserPageToFollower"{ 112 let destinationcontroller = segue.destination as! FollowerViewController 113 114 destinationcontroller.passedTappedUser = NCMBUser.current() 115 } 116 117 if segue.identifier == "fromUserPageToFollowing"{ 118 let destinationcontroller = segue.destination as! FollowingViewController 119 120 destinationcontroller.passedTappedUser = NCMBUser.current() 121 } 122 } 123 124 125 126 127 128 129 130 131 132} 133
print()を使って確認してみたところ、EditProfileViewController.swiftの方は全て呼ばれるべき関数等は呼ばれていましたが、UserPageViewController.swiftの
func didEditProfile(profileText: String) { print("ーーーーーーー呼ばれてる") profileTextView.text = profileText print("ーーーーーーー呼ばれてる") }
の部分は呼ばれていることが確認できません。
また、今はとりあえずプロフィール文のみの変更をモーダルを閉じ次第反映させようとしています。よろしくお願いします。
回答1件
あなたの回答
tips
プレビュー