delegate의 대리자 NO3 swift
그래서 델리게이트와 친해지기 위해 델리게이트를 더 많이 알겠습니다. 그것을 기록에 남겨 두려고 생각하고 여기에 쓰겠습니다. No3

이번에는 본래 xcode에 있는 델리게이트 메소드를 자작한다는 것입니다.
이번에는 쓰는 것이 많이 있고 코멘트 투성이가 되어 버렸기 때문에 보기 어렵게 되어 있으면 미안해.

이번 참고로 한 URL입니다. →
ht tp // ぬけぬけ. 하테나 bぉg. 코m/엔트리/2015/09/17/120749
델리게이트를 자작하기 위해서는 플러스하고 무엇을 하는가?
델리게이트 메소드로 하는 것을 작성한 프로토콜에 선언한다.
@objc protocol SampleViewDelegate {
@objc optional func didChangeBackgroundColor(str:String)
}
대리자 메서드를 사용하기 위해 대리자를 선언합니다.
weak var delegate: SampleViewDelegate?
사용하고 싶은 곳에서 대리자 메서드 호출self.delegate?.didChangeBackgroundColor!(str:"押されました!")
전반적인 코드↓
ViewController.swift
//わたし
import UIKit
//わたしはあなたと一緒じゃないとできないの。
//作成したデリゲートを宣言します。
class ViewController: UIViewController,SampleViewDelegate {
override func viewDidLoad() {
super.viewDidLoad()
//このクラスでのviewにsampleクラスで作ったviewを入れます。
let view = Sample(frame:CGRect(x: 0.0, y: 0.0, width: 300.0, height: 300.0));
//このviewの色々な設定はここ(self)で行います。
//あなたの子はあなたの色々んものを引き継ぐわ
view.delegate = self
//作成したviewをこのクラスが持っているviewに入れます(表示)
self.view.addSubview(view)
}
/*このdidChangeBackgroundColorのメソッドの実行されるタイミングはボタンが押された時です*/
//デリゲートメソッドのカスタマイズ
//あなたをカスタマイズします
func didChangeBackgroundColor(str:String) {
//didChangeBackgroundColorが実行されるタイミングでstrで渡されている引数を使い簡単なログを表示します。
print(str); //押されました!
//didChangeBackgroundColorが実行されるタイミングで自分のviewのバックにあるself.viewの色を青にします。
self.view.backgroundColor = UIColor.blue
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
DelegateSample.swift
//あなたは本来xcodeにいるけど。いないあなたを作る = 自作Delegate
import Foundation
import UIKit
//あなた
// SampleViewDelegate プロトコルを記述 デリゲートとして扱うものはこういう書き方をする。
@objc protocol SampleViewDelegate {
// デリゲートメソッド定義
//optionalをつけることで必ずしも宣言しないといけないデリゲートにはならない。
//optionalの部分がrequiredだと必ず実装しないといけないデリゲートになる。
//@objc optionalがない状態だとrequiredとして扱われるらしいです。
@objc optional func didChangeBackgroundColor(str:String)
}
class Sample : UIView{
//SampleViewDelegateのインスタンスを宣言
//このクラスでデリゲートを使用するので宣言しとく。
weak var delegate: SampleViewDelegate?
override func draw(_ rect: CGRect) {
//ボタンを作成
let button = UIButton()
button.setTitle("Tap", for: .normal)
button.frame = (frame:CGRect(x: 0.0, y: 0.0, width: 300.0, height: 300.0));
button.backgroundColor = UIColor.red
//ボタンが押された時に実行するメソッド作成
button.addTarget(self, action: #selector(tappedButton(sender:)), for: .touchUpInside)
self.addSubview(button)
}
//ボタンが押された時にデリゲートメソッドであるdidChangeBackgroundColorを実行する。
//あなたが呼ばれるタイミング
@IBAction func tappedButton(sender: AnyObject) {
// デリゲートメソッドを呼ぶ(処理をデリゲートインスタンスに委譲する)
//デリゲートにすることで色々なところでカスタマイズできるようになる。
self.delegate?.didChangeBackgroundColor!(str:"押されました!")
}
}
시뮬레이터로 표시하면 이런 느낌입니다.
버튼을 누르기 전

버튼을 누른 후

덧붙여서 물론 밀린 흠집 로그의 "밀렸습니다!"는 발행됩니다.
더 공부하고, 이번에는 참고없이 델리게이트를 쓰고 싶습니다. 열심히! 나!

Reference
이 문제에 관하여(delegate의 대리자 NO3 swift), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/sachiko-kame/items/d8a16148e9ed70074c78텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)