初心者です。
現在xcode(10.1)を使ってアプリ制作をしようと思っております。
「ViewControllerにあるボタンを押すと、テキストフィールド(nameField)に打ち込まれた文字がSecondViewControllerのラベル(nameLabel)に表示される」
という風にしたいと思っているのですが、うまくいきません。
コード import UIKit class ViewController: UIViewController { @IBOutlet weak var nameField: UITextField? override func viewDidLoad(){ super.viewDidLoad() } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Do any additional setup after loading the view, typically from a nib. }//3f override func prepare(for segue: UIStoryboardSegue, sender: Any?){ let SecondViewController = segue.destination as! SecondViewController SecondViewController.text1 = nameField?.text } }
コード import UIKit class SecondViewController: UIViewController{ @IBOutlet weak var nameLabel: UILabel! var text1: String? override func viewDidLoad(){ super.viewDidLoad() nameLabel.text = text1 } // Do any additional setup after loading the view. override func didReceiveMemoryWarning(){ super.didReceiveMemoryWarning() } }
おかしなところばかりだと思います。
勉強不足です。
コードの手直しなどしていただけると幸いです。
どうぞよろしくお願いいたします。
何をしたら、何がどううまくいかないんですか?
これでは答えようがないです。
すみません。
シミュレータを起動して、実際にテキストフィールドに文字を打ち込んでボタンを押すと、「Thread 1: signal SIGABRT」というエラーが出てしまい、
その後テキストフィールドに文字を打ち込んだりボタンを押すことができなくなります。
xcodeのsegueの機能を使って、ボタンを押すとSecondViewControllerに画面を遷移することはできました。
落ちている場所とエラーメッセージを書いて下さい。(エラーメッセージはConsoleに表示されています)
エラーになっているのはViewControllerの
let SecondViewController = segue.destination as! SecondViewController
の部分です。
次の文がエラーメッセージでしょうか?
2019-01-21 15:40:14.747747+0900 studying[35472:4736321] Unknown class SecondViewController in Interface Builder file.
Could not cast value of type 'UIViewController' (0x11239ba10) to 'studying.SecondViewController' (0x10c630598).
(lldb)