This is a sample of a delegate that executes functions, etc. from one View to another ViewController.
In this case, I changed the background color of the original view from a modal view using a delegate.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
protocol SampleDelegate: class { | |
func changeBackgroundColor(color:UIColor) | |
} | |
class SecondViewController: UIViewController { | |
//delegate | |
weak var delegate: SampleDelegate? | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
self.view.backgroundColor = UIColor.gray | |
//ボタンの生成 | |
let deleteModalBtn = UIButton() | |
deleteModalBtn.frame.size = CGSize(width: 150, height: 50) | |
deleteModalBtn.center.x = self.view.center.x | |
deleteModalBtn.center.y = self.view.center.y | |
deleteModalBtn.backgroundColor = UIColor.white | |
deleteModalBtn.addTarget(self, action: #selector(deleteModalBtnClicked(sender:)), for:.touchUpInside) | |
deleteModalBtn.setTitle("モーダル閉じる", for: UIControlState.normal) | |
deleteModalBtn.setTitleColor(UIColor.gray, for: UIControlState.normal) | |
self.view.addSubview(deleteModalBtn) | |
} | |
func deleteModalBtnClicked(sender: UIButton){ | |
//乱数を用いてランダムに色を作成する | |
let red:Float = Float(arc4random_uniform(255))/255 | |
let green:Float = Float(arc4random_uniform(255))/255 | |
let blue:Float = Float(arc4random_uniform(255))/255 | |
let bgColor:UIColor = UIColor(colorLiteralRed: red, green: green, blue: blue, alpha: 1) | |
//デリゲートを用いて初めのViewの色をランダムに変える | |
delegate?.changeBackgroundColor(color: bgColor) | |
//モーダルViewを閉じる | |
self.dismiss(animated: true, completion: nil) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
class ViewController: UIViewController,SampleDelegate { | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
//ボタンの生成 | |
let showModalBtn = UIButton() | |
showModalBtn.frame.size = CGSize(width: 150, height: 50) | |
showModalBtn.center.x = self.view.center.x | |
showModalBtn.center.y = self.view.center.y | |
showModalBtn.backgroundColor = UIColor.gray | |
showModalBtn.addTarget(self, action: #selector(showModalBtnClicked(sender:)), for:.touchUpInside) | |
showModalBtn.setTitle("モーダル表示", for: UIControlState.normal) | |
showModalBtn.setTitleColor(UIColor.white, for: UIControlState.normal) | |
self.view.addSubview(showModalBtn) | |
} | |
func showModalBtnClicked(sender: UIButton){ | |
//モーダルで別ビューを表示する | |
let secondVC:SecondViewController = SecondViewController() | |
secondVC.delegate = self | |
present(secondVC, animated: true, completion: nil) | |
} | |
//Delegateで呼ぶViewの背景色を変えるメソッド | |
func changeBackgroundColor(color:UIColor){ | |
self.view.backgroundColor = color | |
} | |
} |