FirstViewContrllerからSecondViewControllerにsegueで遷移し、同時にStringデータを送りたいのですが、データを送った後にエラーが発生します。
swift3
1import UIKit 2 3class FirstViewController: UIViewController { 4 let sendData = "abcdef" 5 6 @IBAction func backToFirst(segue: UIStoryboardSegue) {} 7 8 @IBAction func firstButton(_ sender: Any) { 9 print("buttonが押されました") 10 11 performSegue(withIdentifier: "nextSegue",sender: self.sendData ) 12 13 } 14 override func viewDidLoad() { 15 super.viewDidLoad() 16 // Do any additional setup after loading the view, typically from a nib. 17 } 18 19 override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 20 if segue.identifier == "nextSegue" { 21 let secondViewController = segue.destination as! SecondViewController 22 secondViewController.parameter = sender as! String 23 } 24 } 25 26 override func didReceiveMemoryWarning() { 27 super.didReceiveMemoryWarning() 28 // Dispose of any resources that can be recreated. 29 } 30 31 32}
swift3
1import UIKit 2 3class SecondViewController: UIViewController { 4 5 var parameter:String = "" 6 7 override func viewDidLoad() { 8 super.viewDidLoad() 9 // Do any additional setup after loading the view, typically from a nib. 10 11 print(parameter) 12 } 13 14 override func didReceiveMemoryWarning() { 15 super.didReceiveMemoryWarning() 16 // Dispose of any resources that can be recreated. 17 } 18}
エラー内容
buttonが押されました
abcdef
Could not cast value of type 'UIButton' (0x3b9c86d8) to 'NSString' (0x3b9b9d4c).
2017-03-15 11:17:05.517679 SegueEX[9478:3287396] Could not cast value of type 'UIButton' (0x3b9c86d8) to 'NSString' (0x3b9b9d4c).
エラー内容を見るとボタン押下とデータ渡しは完了しているように見えるのですが、キャストができないと言われています。どこが悪いのかわかりません。どうかよろしくお願い申し上げます。
回答1件
あなたの回答
tips
プレビュー