0

NSNotification 通知が失敗し、指定したMapが表示されません。(日本地図が表示されます)
コードは以下の通りです。問題点を教えて頂けませんでしょうか。
よろしくお願いいたします。

《やりたいこと》
1.ViewController 条件分岐
2.NSTimer → 3秒後に画面遷移を設定
 NSNotification → userInfoに数値データをもたせ、SecondViewControllerに通知を送る
3.SecondViewController 通知を受け取り、switchで受け取った数値を選択、Mapを表示させる

ViewController.swift

import UIKit

class ViewController: UIViewController {

  var timer:NSTimer = NSTimer()


override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}

@IBAction func didTouchAddButton(sender: AnyObject) {

  let randNum = Int(arc4random_uniform(5))

  if randNum == 0 {
     timer = NSTimer.scheduledTimerWithTimeInterval(3.0, target: self, selector: #selector(ViewController.changeView), userInfo: ["value": 0], repeats: false)

     let n : NSNotification = NSNotification(name: "dummy", object: self, userInfo: ["value": 0])
     NSNotificationCenter.defaultCenter().postNotification(n)


  }else if(randNum == 1){
    timer = NSTimer.scheduledTimerWithTimeInterval(3.0, target: self, selector: #selector(ViewController.changeView), userInfo: ["value": 1], repeats: false)

    let n : NSNotification = NSNotification(name: "dummy", object: self, userInfo: ["value": 1])
    NSNotificationCenter.defaultCenter().postNotification(n)

  //〜〜〜省略〜〜〜

  }else{
    timer = NSTimer.scheduledTimerWithTimeInterval(3.0, target: self, selector: #selector(ViewController.changeView), userInfo:  ["value": 100], repeats: false)

    let n : NSNotification = NSNotification(name: "dummy", object: self, userInfo: ["value": 100])
    NSNotificationCenter.defaultCenter().postNotification(n)
      }
}
func changeView() {
    self.performSegueWithIdentifier("toMap", sender: nil)
 }
}

SecondViewController.swift

import UIKit
import MapKit

class SecondViewController: UIViewController,MKMapViewDelegate {

@IBOutlet weak var mapView: MKMapView!


override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(false)

    NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(SecondViewController.start) ,name: "dummy", object: nil)

}
    func start(notification: NSNotification) {
        if let userInfo = notification.userInfo {
            let result = userInfo["value"]! as! Int

    switch result {

    case 0:
        let coordinate = CLLocationCoordinate2DMake(33.590241, 130.421222)

        let span = MKCoordinateSpanMake(0.01, 0.01)

        let region = MKCoordinateRegionMake(coordinate, span)
        self.mapView.setRegion(region, animated: true)

        let annotation = MKPointAnnotation()
        annotation.coordinate = CLLocationCoordinate2DMake(33.590241, 130.421222)
        annotation.title = "Hakata Station"
        annotation.subtitle = "博多駅"
        self.mapView.addAnnotation(annotation)


    case 1:
        let coordinate = CLLocationCoordinate2DMake(34.985849, 135.758767)

        let span = MKCoordinateSpanMake(0.01, 0.01)

        let region = MKCoordinateRegionMake(coordinate, span)
        self.mapView.setRegion(region, animated: true)

        let annotation = MKPointAnnotation()
        annotation.coordinate = CLLocationCoordinate2DMake(34.985849, 135.758767)
        annotation.title = "Kyoto Station"
        annotation.subtitle = "京都駅"
        self.mapView.addAnnotation(annotation)

    //〜〜〜省略〜〜〜

    default:
        let coordinate = CLLocationCoordinate2DMake(43.068661, 141.350755)

        let span = MKCoordinateSpanMake(0.01, 0.01)

        let region = MKCoordinateRegionMake(coordinate, span)
        self.mapView.setRegion(region, animated: true)

        let annotation = MKPointAnnotation()
        annotation.coordinate = CLLocationCoordinate2DMake(43.068661, 141.350755)
        annotation.title = "Sapporo Station"
        annotation.subtitle = "札幌駅"
        self.mapView.addAnnotation(annotation)
      }
     }
    }
   }
2
  • あなたのコードは、1.閉じブレース}が幾つか不足しているので、正しいクラス定義になっていません、2.SecondViewController.swiftで定義されているクラス名はMapViewControllerなのに#selectorではSecondViewController.startでメソッドを指定しているので、そこでエラーになってしまいます。できれば質問を「編集」して正しいコードにしておいてください。回答の方はそこら辺を「普通ならこうでしょう」と言う推測で補って書いてみますが、もしかしたら修正する必要が出てくるかもしれません。
    – OOPer
    2016年4月14日 7:13
  • Le Pere d'OOさん ありがとうございます。修正しました。 コピーした際に直し忘れていました。申し訳ありません。
    – ari
    2016年4月15日 7:30

1 件の回答 1

1

NSNotificationは、ご質問にあるような画面遷移の際のデータの受け渡しには向いていません。NSNotificationCenter.defaultCenter().postNotification(n)で送られた通知は、その時点で該当の通知を受け取るようなobserverが登録されていなければ無視されます。この時点ではまだ画面遷移が始まりもしていないので、SecondViewControllerについては、通知を受け取るどころか、インスタンスさえ存在しないでしょう。


画面遷移時にViewController間でのデータ連携を行いたいのであれば、例えばprepareForSegue(_:sender:)メソッドを使うのが簡単です。確実に遷移元と遷移先のインスタンスが生成された状態で呼ばれるので、インスタンスプロパティをデータの受け渡しに使用することができます。

SecondViewController.swift:

import UIKit
import MapKit

class SecondViewController: UIViewController, MKMapViewDelegate {

    @IBOutlet weak var mapView: MKMapView!

    //`ViewController`から値を受け取るためのプロパティ
    var valueToReceive: Int = 0

    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(false)

        //インスタンスプロパティの`valueToReceive`に値が入っているので、それをそのまま使うだけ
        start(valueToReceive)
    }

    func start(result: Int) {
        switch result {
        case 0:
            let coordinate = CLLocationCoordinate2DMake(33.590241, 130.421222)
            //以下元のコードと同じ                
        case 1:
            let coordinate = CLLocationCoordinate2DMake(34.985849, 135.758767)
            //以下元のコードと同じ                

            //〜〜〜省略〜〜〜

        default:
            let coordinate = CLLocationCoordinate2DMake(43.068661, 141.350755)
            //以下元のコードと同じ                
        }
    }
}

ViewController.swift:

import UIKit

class ViewController: UIViewController {

    var timer: NSTimer? //使いもしないインスタンスを生成するのは良くないので修正

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    @IBAction func didTouchAddButton(sender: AnyObject) {

        let randNum = Int(arc4random_uniform(5))

        if randNum == 0 {
            timer = NSTimer.scheduledTimerWithTimeInterval(3.0, target: self, selector: #selector(ViewController.changeView), userInfo: ["value": 0], repeats: false)
            valueToSend = 0
        } else if randNum == 1 {
            timer = NSTimer.scheduledTimerWithTimeInterval(3.0, target: self, selector: #selector(ViewController.changeView), userInfo: ["value": 1], repeats: false)
            valueToSend = 1

            //〜〜〜省略〜〜〜

        } else {
            timer = NSTimer.scheduledTimerWithTimeInterval(3.0, target: self, selector: #selector(ViewController.changeView), userInfo:  ["value": 100], repeats: false)
            valueToSend = 100
        }
    }

    func changeView() {
        self.performSegueWithIdentifier("toMap", sender: nil)
    }

    //`SecondViewController`に渡す値を一時的に保持するためのプロパティ    
    private var valueToSend = 0

    // 以下が一番肝心な`prepareForSegue(_:sender:)`
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "toMap" {
            let destinationViewController = segue.destinationViewController as! SecondViewController
            destinationViewController.valueToReceive = valueToSend
        }
    }
}

(ところで何のためにボタンを押してから「3秒後に画面遷移」なんて、ユーザをイライラさせるようなことをするのでしょうか?もし、「データ連携がうまくいかないので試しに時間を取ってみた」だけなら、その部分は不要ですから、さらに簡単になります。)

いろいろな理由で上記のコードそのままは使えないかもしれませんが、とりあえず「画面遷移時のデータの受け渡し」としては確実に動くコードになっています。お試しください。

2
  • ご回答ありがとうございます。上記のコードで動きました。とても分かりやすかったです!
    – ari
    2016年4月15日 7:42
  • @ari さん、ご報告とご質問内のコード修正ありがとうございます。回答の方もそれに合わせて少しだけ(コードに変更はありません)修正しておきます。
    – OOPer
    2016年4月15日 7:48

この質問に回答するには、ログインする必要があります。

求めていた回答ではありませんか? のタグが付いた他の質問を参照する。