[iOS8] Swiftでdelegateを使ったモーダル間の値渡し
モーダルを呼び出し、呼び出し元のコントローラーに値を渡す時のSwiftでの書き方について説明していきます。
サンプルコードはこちらから
https://github.com/eversense/Swift-Delegate-Modal-Example
今回はメインとなるViewとモーダルとなるViewの2つを用意していきます。
・ParentViewController:メインとなる画面(値の受け取り側)
・ModalViewController:モーダルの画面(値の送り側)
まずはモーダル画面のModalViewControllerから見ていきます。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
import UIKit protocol ModalViewControllerDelegate{ func modalDidFinished(modalText: String) } class ModalViewController: UIViewController { var delegate: ParentViewController! = 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をみてみましょう。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
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
Objective-C時代はこのデリゲートの方法が一般的だったかと思います。Swiftになり、さらに別の手段もあるかも。知っている方がいましたらぜひコメントください!