LoginSignup
101
101

More than 5 years have passed since last update.

[iOS8] Swiftでdelegateを使ったモーダル間の値渡し

Last updated at Posted at 2014-12-08

モーダルを呼び出し、呼び出し元のコントローラーに値を渡す時のSwiftでの書き方について説明していきます。

screenshot

サンプルコードはこちらから
https://github.com/eversense/Swift-Delegate-Modal-Example

今回はメインとなるViewとモーダルとなるViewの2つを用意していきます。
・ParentViewController:メインとなる画面(値の受け取り側)
・ModalViewController:モーダルの画面(値の送り側)
まずはモーダル画面のModalViewControllerから見ていきます。

import UIKit

protocol ModalViewControllerDelegate{
    func modalDidFinished(modalText: String)
}

class ModalViewController: UIViewController {

    var delegate: ModalViewControllerDelegate! = nil
    let text1 = UITextField()

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        self.view.backgroundColor = UIColor.orangeColor()

        self.text1.frame = CGRectMake(0, 0, 300, 50)
        self.text1.layer.position = CGPoint(x: self.view.frame.width/2, y:100)
        self.text1.backgroundColor = UIColor.whiteColor()
        self.view.addSubview(self.text1)

        let submitBtn = UIButton(frame: CGRectMake(0, 0, 300, 50))
        submitBtn.layer.position = CGPoint(x: self.view.frame.width/2, y:200)
        submitBtn.setTitle("Submit", forState: .Normal)
        submitBtn.addTarget(self, action: "submit:", forControlEvents: .TouchUpInside)
        self.view.addSubview(submitBtn)


    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func submit(sender: AnyObject) {
        self.delegate.modalDidFinished(self.text1.text)
    }
}

delegateを使うため、protocolでModalViewControllerDelegate設定します。delegateで使うメソッドのmodalDidFinishedを示しておきます。このmodalDidFinishedをParentViewControllerでメソッドを書く必要があります。

Submitのボタンを押すと、modalDidFinishedにtext1のフォームに入力されたテキストを送るようにしてあります。

次にメイン画面のParentViewControllerをみてみましょう。

import UIKit

class ParentViewController: UIViewController, ModalViewControllerDelegate {

    let modalView = ModalViewController()
    let modalTextLabel = UILabel()

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        let showModalBtn = UIButton(frame: CGRectMake(0, 0, 300, 50))
        showModalBtn.layer.position = CGPoint(x: self.view.frame.width/2, y: 100)
        showModalBtn.setTitle("Show Modal", forState: .Normal)
        showModalBtn.setTitleColor(UIColor.blueColor(), forState: .Normal)
        showModalBtn.addTarget(self, action: "showModal:", forControlEvents:.TouchUpInside)
        self.view.addSubview(showModalBtn)

        self.modalTextLabel.frame = CGRectMake(0, 0, 300, 50)
        self.modalTextLabel.layer.position = CGPoint(x: self.view.frame.width/2, y: 200)
        self.modalTextLabel.textAlignment = .Center
        self.modalTextLabel.text = "The Modal text is ..."
        self.modalTextLabel.textColor = UIColor.blackColor()
        self.view.addSubview(modalTextLabel)

        self.modalView.delegate = self

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func showModal(sender: AnyObject){
        self.presentViewController(self.modalView, animated: true, completion: nil)
    }

    func modalDidFinished(modalText: String){
        println(modalText)
        self.modalTextLabel.text = modalText
        self.modalView.dismissViewControllerAnimated(true, completion: nil)
    }
}

showModalBtnをタップすることでモーダル画面を表示させるようにしています。モーダルの呼び出しにはpresentViewControllerを使います。

モーダル側のデリゲートとして設定されたmodalDidFinishedを使い、モーダルの終了処理と渡された値を取得して、処理を書きます。今回はモーダル側のテキストフィールドに書かれたテキストを、メイン側のラベルに入れてみました。

以上が、デリゲートを使ったモーダルの値渡しでした。

サンプルコードはこちらから
https://github.com/eversense/Swift-Delegate-Modal-Example

101
101
6

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
101
101