storyboard上のTextFieldに入力した文字列をボタンを押したときに別画面に設置したTextViewに表示させたい。
ですが、いざ実行してみるとThread 1:signal ...が出るとともに、以下のエラー文も出ました。
fatal error: unexpectedly found nil while unwrapping an Optional value
調べてみると、オプショナル型が原因らしく、nilが変数に代入されているためにエラーが出ているとのことでした。いくつかのオプショナル型のパターンを試しましたが、同じくエラーが出る結果となりました。別の原因としてUIパーツと接続がなされていないとのことでしたが、確認した所そのようなことはありませんでした。
解決策の方をお願いします。
swift
1import UIKit 2 3class ViewController: UIViewController { 4 5 @IBOutlet weak var textField: UITextField? 6 @IBOutlet weak var textLabel: UILabel! 7 8 override func viewDidLoad() { 9 super.viewDidLoad() 10 // Do any additional setup after loading the view, typically from a nib. 11 } 12 13 override func didReceiveMemoryWarning() { 14 super.didReceiveMemoryWarning() 15 // Dispose of any resources that can be recreated. 16 } 17 18 //textViewの画面へ遷移する関数 19 @IBAction func moveButton(sender: UIButton) { 20 21 } 22 23 //textViewに指定したオブジェクトを追加する 24 @IBAction func addButton(sender: UIButton) { 25 //TextViewControllerのインスタンスを作成 26 var textViewController: TextViewController = TextViewController() 27 28 //TextViewControllerのTextViewにtextFieldの文字列を代入 29 textViewController.textView!.text = self.textField!.text 30 31 } 32} 33 34
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。