6
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[iOS][Swift]prepareForSegueをOverride出来ない……の対処法

Posted at

iOS初心者です。iOS開発の触りしか出来ていないので詳しいことはわからないのですが、画面遷移のときにSegueってやつを使って値を次の画面(ViewController)にパスするときにドハマりした話です。今日Xcodeを8.0にアップデートしたのが原因かもしれません。詳しい方は原因や正しい対処法などコメントしていただけると嬉しいです。

prepareForSegueがオーバーライドできなかった

iOS開発入門書籍の解説に従ってコーディングしていました。画面遷移に伴って別のViewControllerに値をパスするコードを記述する部分にさしかかります。どうもprepareForSegueというメソッドをオーバーライドして値をパスする処理を書くようです。

参考記事
Swiftでセグエを利用した画面遷移 - Qiita

そこで次のようなコードを記述します。(上の記事より引用)

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?){
        var secondViewController:SecondViewController = segue.destinationViewController as SecondViewController
        secondViewController.param = self.paramText.text
    }

するとこのようなエラーが出てしまいました。
Method does not override any method from its superclass

原因はよくわからないのですが、スーパークラスにこんなメソッドはないとのこと。言われたとおりにやったんですが…なぜ…

応急処置

とりあえずインテリセンスを頼りに、似た名前のメソッドの定義を見ながら代わりになるそれっぽいメソッドを見つけました。あとsegue.destinationViewControllerってのも使えないっぽいのですが、代わりにsegue.destinationというUIViewController型のプロパティを見つけたのでこれを利用。

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        var secondViewController:SecondViewController = segue.destination as SecondViewController
        secondViewController.param = self.paramText.text
    }

書き換えると上手く機能しました。セフセフ。

6
2
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?