How to Configure Deligates Through Methods

Asked 2 years ago, Updated 2 years ago, 97 views

When setting up TestViewController Deligate on the ViewController,
I'd like to make it happen by mediating the getViewController method in VIewData.
The ViewController topController.myDelegate=self results in a build error as shown in the source below.

Is it wrong to use the delivery method wrong?In the first place
by the method (getViewController) Is it impossible to hand over the delivery?

VIwController.swift

import UIKit

classViewController:UIViewController, myDelegate {
// Read View Data
    var viewdata = ViewData()
     override func viewDidLoad(){
        super.viewDidLoad()   
        var controllers: UIViewController = [ ]
        lettopController=viewdata.getViewController("TEST")
        topController.myDelegate=self//←Build Error Here [Value of type 'UIViewController' has no member 'newsDelegate']
        controllers.append(topController)   
        ~~~~~~
     }
    func myTest(){
            ~~~~~~
    }
}

ViewData.swift

import UIKit
classViewData{

  func getViewController(_text:String) - > UIViewController {
    switch text {
      case "TEST":
        letstoryboard:UIStoryboard=UIStoryboard(name: "Main", bundle:nil)
        let TestView=storyboard.instantiateViewController(withIdentifier: "Test") as!TestViewController
        return TestView

      default:
    }

  }

}

TestViewController.swift

import UIKit
class TestViewController:UITableViewController {
    var myDelegate: myDelegate?

    override func viewDidLoad(){
            super.viewDidLoad()
        ~~~
    }
    ~~~
    func A(){
        myDelegate?.myTest()// I would like to receive the results of this execution in ViewController
    }
}

myDelegate.swift

import UIKit
protocol myDelegate {
  func myTest()
}

swift delegate

2022-09-30 16:44

1 Answers

As the comments say,

topController.myDelegate=self

To avoid compilation errors, the Swift compiler must know that the topController data type is TestViewController.

Since your current code clearly states that the return value of ViewData.getViewController(_:) is UIViewController, Swift recognizes the topController type as UIViewController.

Therefore, you will need to change the topController declaration in the previous line.

let topController=viewdata.getViewController("TEST") as!TestViewController

In Swift's static typing system (types are determined at compile time), impossible to "return non-TestViewController types per case statement" is not "difficult."

Therefore, as!TestViewController in the getViewController(_:) declaration does not have much meaning.

To be honest, the process of instantiating a view controller is not often called in the app, so I don't feel any benefit of defining a separate method like getViewController(_:).

Why did you want to create a method like getViewController(_:) controlled by the switch statement? There may be another solution if you could give me that much.


2022-09-30 16:44

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.