Programming

신속한 대의원?

procodes 2020. 7. 1. 22:22
반응형

신속한 대의원?


NSUserNotificationCenterDelegate신속한 대의원을 만드는 방법은 무엇입니까?


obj-c와 다르지 않습니다. 먼저 다음과 같이 클래스 선언에 프로토콜을 지정해야합니다.

class MyClass: NSUserNotificationCenterDelegate

구현은 다음과 같습니다.

// NSUserNotificationCenterDelegate implementation
func userNotificationCenter(center: NSUserNotificationCenter, didDeliverNotification notification: NSUserNotification) {
    //implementation
}

func userNotificationCenter(center: NSUserNotificationCenter, didActivateNotification notification: NSUserNotification) {
    //implementation
}

func userNotificationCenter(center: NSUserNotificationCenter, shouldPresentNotification notification: NSUserNotification) -> Bool {
    //implementation
    return true
}

물론 델리게이트를 설정해야합니다. 예를 들면 다음과 같습니다.

NSUserNotificationCenter.defaultUserNotificationCenter().delegate = self;

다음은 두 개의 뷰 컨트롤러 사이의 델리게이트에 대한 약간의 도움말입니다.

1 단계 : UIViewController에서 데이터를 제거 / 전송할 프로토콜을 만듭니다.

protocol FooTwoViewControllerDelegate:class {
    func myVCDidFinish(_ controller: FooTwoViewController, text: String)
}

2 단계 : 송신 클래스 (예 : UIViewcontroller)에서 델리게이트 선언

class FooTwoViewController: UIViewController {
    weak var delegate: FooTwoViewControllerDelegate?
    [snip...]
}

3 단계 : 클래스 메서드에서 대리자를 사용하여 프로토콜을 채택하는 모든 메서드 인 수신 메서드로 데이터를 보냅니다.

@IBAction func saveColor(_ sender: UIBarButtonItem) {
        delegate?.myVCDidFinish(self, text: colorLabel.text) //assuming the delegate is assigned otherwise error
}

4 단계 : 수신 클래스의 프로토콜 채택

class ViewController: UIViewController, FooTwoViewControllerDelegate {

5 단계 : 델리게이트 메소드 구현

func myVCDidFinish(_ controller: FooTwoViewController, text: String) {
    colorLabel.text = "The Color is " +  text
    controller.navigationController.popViewController(animated: true)
}

6 단계 : PreparingForSegue에서 대리인을 설정합니다.

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "mySegue" {
        let vc = segue.destination as! FooTwoViewController
        vc.colorString = colorLabel.text
        vc.delegate = self
    }
}

그리고 그것은 효과가 있습니다. 이것은 물론 코드 조각 일뿐이지만 아이디어를 제공해야합니다. 이 코드에 대한 자세한 설명은 내 블로그 항목으로 이동하십시오.

말과 대의원

대의원과 함께하는 일에 관심이 있다면 여기에 썼습니다.

대표자들과 함께


델리게이트는 다른 클래스를 위해 일하는 클래스 일 뿐이라는 것을 깨달을 때까지 항상 혼란 스러웠다 . 그것은 당신이 스스로하고 싶지 않은 더러운 일을하도록 다른 사람을 갖는 것과 같습니다.

나는 이것을 설명하기 위해 작은 이야기를 썼습니다. 원한다면 운동장에서 읽어보십시오.

옛날 옛적에...

// MARK: Background to the story

// A protocol is like a list of rules that need to be followed.
protocol OlderSiblingDelegate: class {
    // The following command (ie, method) must be obeyed by any 
    // underling (ie, delegate) of the older sibling.
    func getYourNiceOlderSiblingAGlassOfWater()
}

// MARK: Characters in the story

class BossyBigBrother {

    // I can make whichever little sibling is around at 
    // the time be my delegate (ie, slave)
    weak var delegate: OlderSiblingDelegate?

    func tellSomebodyToGetMeSomeWater() {
        // The delegate is optional because even though 
        // I'm thirsty, there might not be anyone nearby 
        // that I can boss around.
        delegate?.getYourNiceOlderSiblingAGlassOfWater()
    }
}

// Poor little sisters have to follow (or at least acknowledge) 
// their older sibling's rules (ie, protocol)
class PoorLittleSister: OlderSiblingDelegate {

    func getYourNiceOlderSiblingAGlassOfWater() {
        // Little sis follows the letter of the law (ie, protocol),
        // but no one said exactly how she had to respond.
        print("Go get it yourself!")
    }
}

// MARK: The Story

// Big bro is laying on the couch watching basketball on TV.
let bigBro = BossyBigBrother()

// He has a little sister named Sally.
let sally = PoorLittleSister()

// Sally walks into the room. How convenient! Now big bro 
// has someone there to boss around.
bigBro.delegate = sally

// So he tells her to get him some water.
bigBro.tellSomebodyToGetMeSomeWater()

// Unfortunately no one lived happily ever after...

// The end.

검토에서 델리게이트 패턴을 만들고 사용하기위한 세 가지 주요 부분이 있습니다.

  1. 작업자가해야 할 일을 정의 하는 프로토콜
  2. 보스 클래스 무엇가 사용하는 위임 변수를 가지고, 노동자 계급에게
  3. 프로토콜을 채택하고 필요한 작업을 수행 하는 작업자 클래스

현실

위의 Bossy Big Brother 이야기와 비교하여 델리게이트는 종종 다음과 같은 실용적인 응용 프로그램에 사용됩니다.

  1. 의사 소통 : 한 클래스는 다른 클래스로 정보를 보내야합니다.
  2. 커스터마이제이션 : 한 클래스는 다른 클래스가 그것을 커스터마이징하도록 허용하려고합니다.

가장 중요한 부분은 델리게이트 클래스가 필요한 프로토콜을 준수한다는 점을 제외하고는이 클래스들이 서로에 대해 아무것도 알 필요가 없다는 것입니다.

다음 두 기사를 읽는 것이 좋습니다. 그들은 문서 보다 델리게이트를 더 잘 이해하도록 도와주었습니다 .

하나 더 참고

소유하지 않은 다른 클래스를 참조하는 대리인은 weak강력한 참조주기를 피하기 위해 키워드를 사용해야합니다 . 자세한 내용은 이 답변 을 참조하십시오.


@MakeAppPie의 게시물에 대한 수정 사항이 거의 없습니다.

먼저 위임 프로토콜을 만들 때 클래스 프로토콜을 준수해야합니다. 아래 예와 같이.

protocol ProtocolDelegate: class {
    func myMethod(controller:ViewController, text:String)
}

둘째, 유지주기를 피하려면 대리인이 약해야합니다.

class ViewController: UIViewController {
    weak var delegate: ProtocolDelegate?
}

마지막으로, 프로토콜은 선택적인 값이므로 안전합니다. 즉, "nil"메시지가이 속성으로 전송되지 않습니다. respondToselectorobjC 있는 조건문 비슷하지만 여기에는 한 줄로 모든 것이 있습니다.

if ([self.delegate respondsToSelector:@selector(myMethod:text:)]) {
    [self.delegate myMethod:self text:@"you Text"];
}

위에는 obj-C 예제가 있고 아래에는 Swift 예제가 있습니다.

delegate?.myMethod(self, text:"your Text")

여기 내가 정리 요점 이있다. 나는 똑같은 것을 궁금해했고 이것은 내 이해를 향상시키는 데 도움이되었습니다. Xcode Playground 에서 열어서 무슨 일이 일어나고 있는지 확인하십시오.

protocol YelpRequestDelegate {
    func getYelpData() -> AnyObject
    func processYelpData(data: NSData) -> NSData
}

class YelpAPI {
    var delegate: YelpRequestDelegate?

    func getData() {
        println("data being retrieved...")
        let data: AnyObject? = delegate?.getYelpData()
    }

    func processYelpData(data: NSData) {
        println("data being processed...")
        let data = delegate?.processYelpData(data)
    }
}

class Controller: YelpRequestDelegate {
    init() {
        var yelpAPI = YelpAPI()
        yelpAPI.delegate = self
        yelpAPI.getData()
    }
    func getYelpData() -> AnyObject {
        println("getYelpData called")
        return NSData()
    }
    func processYelpData(data: NSData) -> NSData {
        println("processYelpData called")
        return NSData()
    }
}

var controller = Controller()

스위프트 2의 대표단

두 개의 viewController가있는 Delegate의 예를 설명하고 있습니다.이 경우 SecondVC Object는 데이터를 첫 번째 View Controller로 다시 보냅니다.

프로토콜 선언 클래스

protocol  getDataDelegate  {
    func getDataFromAnotherVC(temp: String)
}


import UIKit
class SecondVC: UIViewController {

    var delegateCustom : getDataDelegate?
    override func viewDidLoad() {
        super.viewDidLoad()
     }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    @IBAction func backToMainVC(sender: AnyObject) {
      //calling method defined in first View Controller with Object  
      self.delegateCustom?.getDataFromAnotherVC("I am sending data from second controller to first view controller.Its my first delegate example. I am done with custom delegates.")
        self.navigationController?.popViewControllerAnimated(true)
    }

}

첫 번째 ViewController 프로토콜에서 준수는 다음과 같습니다.

class ViewController: UIViewController, getDataDelegate

First View Controller (ViewController)의 프로토콜 메소드 정의

func getDataFromAnotherVC(temp : String)
{
  // dataString from SecondVC
   lblForData.text = dataString
}

First View Controller (ViewController)에서 SecondVC를 푸시하는 동안

let objectPush = SecondVC()
objectPush.delegateCustom = self
self.navigationController.pushViewController(objectPush, animated: true)

최상위:

protocol NetworkServiceDelegate: class {

    func didCompleteRequest(result: String)
}


class NetworkService: NSObject {

    weak var delegate: NetworkServiceDelegate?

    func fetchDataFromURL(url : String) {
        delegate?.didCompleteRequest(url)
    }
}

이급:

class ViewController: UIViewController, NetworkServiceDelegate {

    let network = NetworkService()

    override func viewDidLoad() {
        super.viewDidLoad()
        network.delegate = self
        network.fetchDataFromURL("Success!")
    }



    func didCompleteRequest(result: String) {
        print(result)
    }


}

간단한 예 :

protocol Work: class {
    func doSomething()
}

class Manager {
    weak var delegate: Work?
    func passAlong() {
        delegate?.doSomething()
    }
}

class Employee: Work {
    func doSomething() {
        print("Working on it")
    }
}

let manager = Manager()
let developer = Employee()
manager.delegate = developer
manager.passAlong() // PRINTS: Working on it

매우 쉬운 단계별 작업 (100 % 작업 및 테스트)

1 단계 : 첫 번째보기 컨트롤러에서 메소드 작성

 func updateProcessStatus(isCompleted : Bool){
    if isCompleted{
        self.labelStatus.text = "Process is completed"
    }else{
        self.labelStatus.text = "Process is in progress"
    }
}

2 단계 : 두 번째 뷰 컨트롤러로 푸시하면서 델리게이트 설정

@IBAction func buttonAction(_ sender: Any) {

    let secondViewController = self.storyboard?.instantiateViewController(withIdentifier: "secondViewController") as! secondViewController
    secondViewController.delegate = self
    self.navigationController?.pushViewController(secondViewController, animated: true)
}

3 단계 : 대표를 다음과 같이 설정

ViewController 클래스 : UIViewController, ProcessStatusDelegate {

4 단계 : 프로토콜 생성

protocol ProcessStatusDelegate:NSObjectProtocol{
func updateProcessStatus(isCompleted : Bool)
}

step5 : 변수를 가져 가라

var delegate:ProcessStatusDelegate?

step6: While go back to previous view controller call delegate method so first view controller notify with data

@IBAction func buttonActionBack(_ sender: Any) {
    delegate?.updateProcessStatus(isCompleted: true)
    self.navigationController?.popViewController(animated: true)
}

@IBAction func buttonProgress(_ sender: Any) {
    delegate?.updateProcessStatus(isCompleted: false)
    self.navigationController?.popViewController(animated: true)

}

Delegates are a design pattern that allows one object to send messages to another object when a specific event happens. Imagine an object A calls an object B to perform an action. Once the action is complete, object A should know that B has completed the task and take necessary action, this can be achieved with the help of delegates! Here is a tutorial implementing delegates step by step in swift 3

Tutorial Link


The solutions above seemed a little coupled and at the same time avoid reuse the same protocol in other controllers, that's why I've come with the solution that is more strong typed using generic type-erasure.

@noreturn public func notImplemented(){
    fatalError("not implemented yet")
}


public protocol DataChangedProtocol: class{
    typealias DataType

    func onChange(t:DataType)
}

class AbstractDataChangedWrapper<DataType> : DataChangedProtocol{

    func onChange(t: DataType) {
        notImplemented()
    }
}


class AnyDataChangedWrapper<T: DataChangedProtocol> : AbstractDataChangedWrapper<T.DataType>{

    var base: T

    init(_ base: T ){
        self.base = base
    }

    override func onChange(t: T.DataType) {
        base.onChange(t)
    }
}


class AnyDataChangedProtocol<DataType> : DataChangedProtocol{

    var base: AbstractDataChangedWrapper<DataType>

    init<S: DataChangedProtocol where S.DataType == DataType>(_ s: S){
        self.base = AnyDataChangedWrapper(s)
    }

    func onChange(t: DataType) {
        base.onChange(t)
    }
}



class Source : DataChangedProtocol {
    func onChange(data: String) {
        print( "got new value \(data)" )
    }
}


class Target {
    var delegate: AnyDataChangedProtocol<String>?

    func reportChange(data:String ){
        delegate?.onChange(data)
    }
}


var source = Source()
var target = Target()

target.delegate = AnyDataChangedProtocol(source)
target.reportChange("newValue")    

output: got new value newValue


In swift 4.0

Create a delegate on class that need to send some data or provide some functionality to other classes

Like

protocol GetGameStatus {
    var score: score { get }
    func getPlayerDetails()
}

After that in the class that going to confirm to this delegate

class SnakesAndLadders: GetGameStatus {
    func getPlayerDetails() {

 }
}

참고URL : https://stackoverflow.com/questions/24099230/delegates-in-swift

반응형