import
1 2class SettingsViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate, UITextFieldDelegate { 3 4 5 @IBOutlet weak var usericonView: UIImageView! 6 7 @IBOutlet weak var nickNameTextField: UITextField! 8 9 @IBOutlet weak var userNameTextField: UITextField! 10 11 @IBOutlet weak var sendEmailTextField: UITextField! 12 13 @IBOutlet weak var sendPhoneNumber: UITextField! 14 15 @IBOutlet weak var nextSendEmailTextField: UITextField! 16 17 @IBOutlet weak var nextSendPhoneNumber: UITextField! 18 19 20 21 @IBOutlet weak var nickNameLabel: UILabel! 22 23 @IBOutlet weak var userNameLabel: UILabel! 24 25 @IBOutlet weak var sendEmailLabel: UILabel! 26 27 @IBOutlet weak var sendPhoneLabel: UILabel! 28 29 @IBOutlet weak var nextSendEmailLabel: UILabel! 30 31 @IBOutlet weak var nextSendPhoneLabel: UILabel! 32 33 34 var nickNameText:String = "ニックネームを入力" 35 var userNameText:String = "名前を入力" 36 var sendEmailText:String = "送り先Emailを入力" 37 var sendPhone:String = "電話番号を入力" 38 var nextSendEmailText:String = "送り先Email-2を入力" 39 var nextSendPhone:String = "電話番号-2を入力" 40 41 42 let userDefaults = UserDefaults.standard 43 44 45 46 override func viewDidLoad() { 47 super.viewDidLoad() 48 49 50 51 nickNameTextField.delegate = self 52 userNameTextField.delegate = self 53 sendEmailTextField.delegate = self 54 sendPhoneNumber.delegate = self 55 nextSendEmailTextField.delegate = self 56 nextSendPhoneNumber.delegate = self 57 58 userDefaults.register(defaults: ["NickName": "ニックネームを入力"]) 59 nickNameLabel.text = readData(key: "NickName") 60 61 userDefaults.register(defaults: ["UserName": "名前を入力"]) 62 userNameLabel.text = readData(key: "UserName") 63 64 userDefaults.register(defaults: ["SendEmail": "Emailを入力"]) 65 sendEmailLabel.text = readData(key: "SendEmail") 66 67 userDefaults.register(defaults: ["SendPhone": "電話番号を入力"]) 68 sendPhoneLabel.text = readData(key: "SendPhone") 69 70 userDefaults.register(defaults: ["NextSendEmail": "Emailを入力"]) 71 nextSendEmailLabel.text = readData(key: "nextSendEmail") 72 73 userDefaults.register(defaults: ["NextSendPhone": "電話番号を入力"]) 74 nextSendPhoneLabel.text = readData(key: "NextSendPhone") 75 76 } 77 78 //アルバムの写真を選択してアイコンとして表示する。 79 @IBAction func toAlbumButton(_ sender: Any) { 80 81 let picker = UIImagePickerController() 82 picker.sourceType = .photoLibrary 83 picker.delegate = self 84 present(picker, animated: true) 85 self.present(picker, animated: true, completion: nil) 86 87 } 88 89 func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) { 90 if let selectedImage = info[.originalImage] as? UIImage{ 91 usericonView.image = selectedImage 92 } 93 self.dismiss(animated: true) 94 } 95 96 func imagePickerControllerDidCancel(_ picker: UIImagePickerController) { 97 self.dismiss(animated: true, completion: nil) 98 } 99 100 //テキスト入力設定 101 102 func textFieldShouldReturn(_ textField: UITextField) -> Bool{ 103 nickNameText = textField.text! 104 nickNameLabel.text = nickNameText 105 106 userNameText = textField.text! 107 userNameLabel.text = userNameText 108 109 sendEmailText = textField.text! 110 sendEmailLabel.text = sendEmailText 111 112 sendPhone = textField.text! 113 sendPhoneLabel.text = sendPhone 114 115 nextSendEmailText = textField.text! 116 nextSendEmailLabel.text = nextSendEmailText 117 118 nextSendPhone = textField.text! 119 nextSendPhoneLabel.text = nextSendPhone 120 121 122 textField.resignFirstResponder() 123 124 saveData(str: nickNameText) 125 saveData(str: userNameText) 126 saveData(str: sendEmailText) 127 saveData(str: sendPhone) 128 saveData(str: nextSendEmailText) 129 saveData(str: nextSendPhone) 130 131 return true 132 133 } 134 135 func saveData(str: String){ 136 userDefaults.set(str, forKey: "NickName") 137 userDefaults.set(str, forKey: "UserName") 138 userDefaults.set(str, forKey: "SendEmail") 139 userDefaults.set(str, forKey: "SendPhone") 140 userDefaults.set(str, forKey: "NextSendEmail") 141 userDefaults.set(str, forKey: "NextSendPhone") 142 } 143 144 func readData(key: String) -> String { 145 let config = UserDefaults.standard 146 let str: String = config.string(forKey: key) ?? "" 147 return str 148 } 149 150 151 @IBAction func hozonButtton(_ sender : AnyObject) { 152 153 154 userDefaults.set(true, forKey: "NickName") 155 let str: String = userDefaults.object(forKey: "NickName") as! String 156 nickNameLabel.text = str 157 158 userDefaults.set(true, forKey: "UserName") 159 let _: String = userDefaults.object(forKey: "UserName") as! String 160 nickNameLabel.text = str 161 162 userDefaults.set(true, forKey: "SendEmail") 163 let _: String = userDefaults.object(forKey: "SendEmail") as! String 164 nickNameLabel.text = str 165 166 userDefaults.set(true, forKey: "SendPhone") 167 let _: String = userDefaults.object(forKey: "SendPhone") as! String 168 nickNameLabel.text = str 169 170 userDefaults.set(true, forKey: "NextSendemail") 171 let _: String = userDefaults.object(forKey: "NextSendEmail") as! String 172 nickNameLabel.text = str 173 174 userDefaults.set(true, forKey: "NextSendPhone") 175 let _: String = userDefaults.object(forKey: "NextSendPhone") as! String 176 nickNameLabel.text = str 177 178 nickNameLabel.text = str 179 userNameLabel.text = str 180 sendEmailLabel.text = str 181 sendPhoneLabel.text = str 182 nextSendEmailLabel.text = str 183 nextSendPhoneLabel.text = str 184 185 186 print() 187 188 } 189 190 191} 192 193コード 194```![textFieldに値を入れると全てのtextLabelに同じ値が入ってします。また、保存ボタンを押すとエラーになります。](fc099d3ae611e40a9da09adf72cc2b13.png)```import UIKit 195 196@UIApplicationMain 197class AppDelegate: UIResponder, UIApplicationDelegate { 198 199 200 201 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { 202 // Override point for customization after application launch. 203 return true 204 } 205 206 // MARK: UISceneSession Lifecycle 207 208 func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration { 209 // Called when a new scene session is being created. 210 // Use this method to select a configuration to create the new scene with. 211 return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role) 212 } 213 214 func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) { 215 // Called when the user discards a scene session. 216 // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions. 217 // Use this method to release any resources that were specific to the discarded scenes, as they will not return. 218 } 219 220 221} 222 223 224コード 225```![入力画面![エラー画面](746c4177f0815a6990fb614a1190fcd3.png)](5c48c61b259040ad9cefd59d6847adf6.png)> ビルドは上手く行ったけど保存ボタンは押された後に 226エラーになります。 227hread 1: Exception: "-[KidsSosApp.SettingsViewController hozonButton:]: unrecognized selector sent to instance 0x7ff6a3832600" 228です。 229### 前提・実現したいこと 230 231ここに質問の内容を詳しく書いてください。 232textfieldに入れた値をtextLabelに表示させて保存する。というのを作成中です。 233それぞれのtextFieldの値をtextLabelに入れたいのに最後に入れた値が全てのtextLabelに 234はいってしまします。 235また、保存ボタンを押すと表題のエラーコードが発生しました。 236 237### 発生している問題・エラーメッセージ 238
hread 1: Exception: "-[KidsSosApp.SettingsViewController hozonButton:]: unrecognized selector sent t
### 該当のソースコード ```ここに言語名を入力 ソースコード
試したこと
ここに問題に対して試したことを記載してください。
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。
回答3件
あなたの回答
tips
プレビュー