2017-09-09 13 views
1

私はこの方法を使用して別のビューコントローラからデータを渡すためにしようとしています:なぜインスタンスメンバー '___'はprepareForSegueの型 '___'に使用できませんか?

func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) { 

     if segue.identifier == "toSecondViewController" { 
      let secondViewController = segue.destination as! SecondViewController 

      SecondViewController.username = "Austin" 
     } 

    } 

しかし、私はエラーに

を得ているインスタンスメンバ「ユーザ名」タイプ に使用することはできません「SecondViewController」

変数usernameSecondViewControllerで定義されているとセグエは確かtoSecondViewControllerという名前です。このエラーの原因は何ですか?

答えて

1

これは私にとって常に起こります。

if segue.identifier == "toSecondViewController" { 
      let secondViewController = segue.destination as! SecondViewController 

      SecondViewController.username = "Austin" 
     } 

SecondViewController.username = "Austin"には大文字のSが含まれています。小文字にする必要があります。

あなたは、変数let secondViewController as! SecondViewController を宣言したが、その後、あなたはあなたが変数secondViewControllerに.userNameを設定する必要があり、資本S

、SecondViewControllerに.userNameを設定してみてください。あなたが意図した小文字のs。

+0

Martin ..今、エラーは発生していませんが、データがSecondViewControllerに渡されているようではありません。私はそれをテストできる簡単な方法はありますか? –

+0

secondViewControllerでは、ユーザー名の宣言方法を教えてください。 var username =あなたはここで何を宣言しましたか? letを宣言した可能性があります。つまり、変更できません。 –

+0

SecondViewControllerで私はvar username = ""を宣言しました –

関連する問題