Buttonを押したら画面遷移せずに落ちてしまい下記のようなエラーが出てしまいます。
Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<MyApp9_0.TestViewController 0x7fca49552910> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key sampleText.'
中略
terminating with uncaught exception of type NSException
エラーの内容を調べてもsegueやOutletの接続の問題と書いてありますが、何回確認してもやり直しても接続できています。
下記の接続方法で試しましたが、やはり落ちてしまいます。他に見るべき点があるのでしょうか?
初歩的な問題で申し訳ございませんが、ご指摘いただければ幸いです。
ちなみにButtonを押す画面の前の画面では問題なく画面遷移できます。
①Buttonから画面遷移先へドラッグ&ドロップ
②画面遷移前のViewControllerでButtonのOutlet及びActionを設定。また、Action内に
self.performSegue(withIdentifier: "画面遷移先のIdentifier", sender: nil)
を入力及びsegueのIdentifierを設定
Swift
1// TestViewController.swift 2 3import UIKit 4import Firebase 5 6class TestViewController: UIViewController,UITextFieldDelegate { 7 8 @IBOutlet weak var sampleLabel: UILabel! 9 10 @IBOutlet weak var inputTextField: UITextField! 11 12 @IBOutlet weak var sendButton: UIButton! 13 14 var testArray = [Sample]() 15 16 let screenSize = UIScreen.main.bounds.size 17 18 19 override func viewDidLoad() { 20 super.viewDidLoad() 21 22 inputTextField.delegate = self 23 24 NotificationCenter.default.addObserver(self, selector: #selector(TestViewController.keyboardWillShow(_ :)), name: UIResponder.keyboardWillShowNotification, object: nil) 25 26 NotificationCenter.default.addObserver(self, selector: #selector(TestViewController.keyboardWillHide(_ :)), name: UIResponder.keyboardWillHideNotification, object: nil) 27 28 } 29 30 //キーボードを開く 31 @objc func keyboardWillShow(_ notification:NSNotification){ 32 33 let keyboardHeight = ((notification.userInfo![UIResponder.keyboardFrameEndUserInfoKey] as Any) as AnyObject).cgRectValue.height 34 35 inputTextField.frame.origin.y = screenSize.height - keyboardHeight - inputTextField.frame.height 36 37 38 } 39 40 //キーボードを閉じる 41 @objc func keyboardWillHide(_ notification:NSNotification){ 42 43 inputTextField.frame.origin.y = screenSize.height - inputTextField.frame.height 44 45 guard let rect = (notification.userInfo?[UIResponder.keyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue, 46 let duration = notification.userInfo?[UIResponder.keyboardAnimationDurationUserInfoKey] as? TimeInterval else{return} 47 UIView.animate(withDuration: duration) { 48 49 50 let transform = CGAffineTransform(translationX: 0, y: 0) 51 52 self.view.transform = transform 53 54 } 55 } 56 57 //TextFieldを押した時 58 override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 59 60 inputTextField.resignFirstResponder() 61 62 } 63 64 //TextFieldでEnterを押した時 65 func textFieldShouldReturn(_ textField: UITextField) -> Bool { 66 67 textField.resignFirstResponder() 68 return true 69 70 } 71 72 73 74 //sendを押した時にfirebaseへ保存 75 @IBAction func sendAction(_ sender: Any) { 76 77 inputTextField.endEditing(true) 78 inputTextField.isEnabled = false 79 sendButton.isEnabled = false 80 81 let testDB = Database.database().reference().child("tests") 82 83 let inputInfo = ["sender":Auth.auth().currentUser?.email,"sample":sampleLabel.text!,"input":inputTextField.text!] 84 85 testDB.childByAutoId().setValue(inputInfo) { (error, result) in 86 87 if error != nil{ 88 print(error) 89 }else{ 90 print("送信完了しました!") 91 self.inputTextField.isEnabled = true 92 self.sendButton.isEnabled = true 93 self.inputTextField.text = "" 94 } 95 96 } 97 98 } 99 100 //Firebaseから取り出す 101 func fetchTestData(){ 102 103 let fetchDataRef = Database.database().reference().child("tests") 104 105 fetchDataRef.observe(.childAdded) { (snapShot) in 106 let snapShotData = snapShot.value as AnyObject 107 let input = snapShotData.value(forKey: "input") 108 let sample = snapShotData.value(forKey: "sample") 109 let sender = snapShotData.value(forKey: "sender") 110 111 let samples = Sample() 112 samples.input = input as! String 113 samples.sample = sample as! String 114 samples.sender = sender as! String 115 self.testArray.append(samples) 116 //リロードが必要 117 } 118 119 } 120 121// inputデータのみを取得したい(未完成) 122// func fetchTestDataInLabel(){ 123// 124// let fetchDataRef = Database.database().reference().child("tests") 125// 126// fetchDataRef.observe(.childChanged) { (snapShot) in 127// let snapShotData = snapShot.value as AnyObject 128// let sample = snapShotData.value(forKey: "sample") 129// } 130// } 131 132 /* 133 // MARK: - Navigation 134 135 // In a storyboard-based application, you will often want to do a little preparation before navigation 136 override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 137 // Get the new view controller using segue.destination. 138 // Pass the selected object to the new view controller. 139 } 140 */ 141 142} 143
回答1件
あなたの回答
tips
プレビュー