Skip to content

Instantly share code, notes, and snippets.

@KentarouKanno
Created August 6, 2016 03:54
Show Gist options
  • Save KentarouKanno/039b676a72d878cf43c81f424a86ff10 to your computer and use it in GitHub Desktop.
Save KentarouKanno/039b676a72d878cf43c81f424a86ff10 to your computer and use it in GitHub Desktop.
画面間の値渡し

画面間の値渡し

参考Project: PassByValue

★ 遷移元 Delegate、Closure、Notification

import UIKit

enum NextViewController: Int {
    case vcB,vcC,vcD
}

class ViewControllerA: UIViewController, ViewControllerBDelegate {
    
    @IBOutlet weak var resultLabel: UILabel!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // 通知を登録
        NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(self.update(_:)), name: "TextUpdateNotification", object: nil)
    }
    
    deinit {
        // 通知を解除
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }
    
    // 画面遷移
    @IBAction func pushNext(sender: UIButton) {
        var segueID: String
        switch NextViewController(rawValue: sender.tag)! {
        case .vcB: segueID = "delegate"
        case .vcC: segueID = "closure"
        case .vcD: segueID = "notification"
        }
        performSegueWithIdentifier(segueID, sender: nil)
    }
    
    // Call Notification
    func update(notification: NSNotification?) {
        setLabel(notification?.object as! String)
    }
    
    func setLabel(str: String) {
        resultLabel.text = str
    }
    
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        
        if let next = segue.destinationViewController as? ViewControllerB  {
            // Delegateを自身に設定
            next.delegate = self
        }
        
        if let next = segue.destinationViewController as? ViewControllerC  {
            next.closuer = { (text) in
                // Closure で呼ばれた値をラベルに設定
                self.setLabel(text)
            }
        }
    }
}

★ 遷移先 Delegate

import UIKit

// Protocol宣言
protocol ViewControllerBDelegate: class {
    func setLabel(str: String)
}

class ViewControllerB: UIViewController {
    
    @IBOutlet weak var textField: UITextField!
    
    // Delegate Instance
    weak var delegate: ViewControllerBDelegate!
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        view.endEditing(true)
    }
    
    @IBAction func back(sender: UIButton) {
        
        // Call Delegate Method
        delegate?.setLabel(textField.text!)
        self.navigationController?.popViewControllerAnimated(true)
    }
}

★ 遷移先 Closure

import UIKit

class ViewControllerC: UIViewController {
    
    // Closure Instance
    var closuer: (String -> ())!
    
    @IBOutlet weak var textField: UITextField!
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        view.endEditing(true)
    }
    
    @IBAction func back(sender: UIButton) {
        
        // Closure
        closuer(textField.text!)
        self.navigationController?.popViewControllerAnimated(true)
    }
}

★ 遷移先 Notification

import UIKit

class ViewControllerD: UIViewController {

    @IBOutlet weak var textField: UITextField!
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        view.endEditing(true)
    }
    
    @IBAction func back(sender: UIButton) {
        
        // 通知を送信
        NSNotificationCenter.defaultCenter().postNotificationName("TextUpdateNotification", object: textField.text!)
        self.navigationController?.popViewControllerAnimated(true)
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment