NSNotification 異なるクラスでの処理を連携させたい
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)
}
}
}
}