画面遷移先のクラス(MusicPostViewController) のquestionPostLabelといラベルのtext にこの遷移元のクラス(MusicViewController)のquestionLabelのテキストを渡したいです。
segueで渡そうと思い@IBAction func toMusicpost()で以下のように書いたのですが、Thread 1: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional valueというエラーが出てしまいます。
これは予期せぬエラーが見つかったということなのでas! Stringと書いてみましたが変化なしでした。。
接続されていることも確認しましたが、どういった対処が必要でしょうか。
何かヒント頂けると幸いです。
class MusicViewController: UIViewController ,IndicatorInfoProvider { //ここがボタンのタイトルに利用されます var itemInfo: IndicatorInfo = "音楽" var qestionlabel :String = "" @IBOutlet weak var questionLabel: UILabel! @IBOutlet weak var label: UILabel! override func viewDidLoad() { super.viewDidLoad() questionLabel.text = "オススメの曲は?" } //必須 func indicatorInfo(for pagerTabStripController: PagerTabStripViewController) -> IndicatorInfo { return itemInfo } @IBAction func toMusicpost() { performSegue(withIdentifier: "toMusicPost", sender: nil) } override func prepare(for segue: UIStoryboardSegue, sender: Any?) { if segue.identifier == "toMusicPost"{ let musicPostViewControllrt = segue.destination as! MusicPostViewController //ここでThread 1: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional valueのエラー musicPostViewControllrt.questionPostLabel.text = questionLabel.text as! String } } }
解決
遷移先(MusicPostViewController)のviewdidloadにquestionPostLabel.text = questionlabelと表示しnilが入らないようにして、そのquestionlabelに遷移元で表示したquestionable(=questionLabel.text)を代入することで成功しました!
回答者の皆様本当にありがとうございました!!
遷移元
class MusicViewController: UIViewController ,IndicatorInfoProvider { //ここがボタンのタイトルに利用されます var itemInfo: IndicatorInfo = "音楽" var questionlabel:String? @IBOutlet weak var questionLabel: UILabel! @IBOutlet weak var label: UILabel! override func viewDidLoad() { super.viewDidLoad() questionLabel.text = "オススメの曲は?" } //必須 func indicatorInfo(for pagerTabStripController: PagerTabStripViewController) -> IndicatorInfo { return itemInfo } @IBAction func toMusicpost() { //questionlabelにquestionLabel.text代入 questionlabel = questionLabel.text performSegue(withIdentifier: "toMusicPost", sender: nil) } override func prepare(for segue: UIStoryboardSegue, sender: Any?) { if segue.identifier == "toMusicPost"{ let musicPostViewControllr = segue.destination as! MusicPostViewController musicPostViewControllr.questionlabel = questionlabel } } }
遷移先
class MusicPostViewController: UIViewController,UINavigationControllerDelegate, UIImagePickerControllerDelegate, UITextViewDelegate { let placeholderImage = UIImage(named: "photo-placeholder") let musicViewController = MusicViewController() var resizedImage: UIImage! //questionlabe保持 var questionlabel:String? @IBOutlet weak var questionPostLabel: UILabel! @IBOutlet var postImageView: UIImageView! @IBOutlet var postTextView: UITextView! @IBOutlet var postButton: UIBarButtonItem! override func viewDidLoad() { super.viewDidLoad() postTextView.delegate = self //初期値にquestionlabelが入る questionPostLabel.text = questionlabel }
回答1件
あなたの回答
tips
プレビュー