Skip to content

Calling a method from another View in a delegate

   

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.

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)
}
}
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
}
}

  1. Get and display the class name, function name and line number in Swift
  2. Selecting images with UIImagePickerController
  3. Playing music with Swift
  4. Array(Ruby)
  5. Whitening the UIStatusBar
  6. UITableView and UISearchBar
  7. UserNotification(LocalNotification)