简体   繁体   English

我的代表 function 没有在 Swift 中被调用

[英]My delegate function is not being called in Swift

I have a main view titled NoteTakerViewController.我有一个名为 NoteTakerViewController 的主视图。 I have a weatherGetterController class with a protocol that returns 5 days of weather with a protocol function called getMyWeather.我有一个weatherGetterController class,其协议返回5天的天气,协议function称为getMyWeather。 However the protocol function is not being called that returns the weather data to NoteTakerViewController.然而,没有调用将天气数据返回给 NoteTakerViewController 的协议 function。 I am certain I have everything set up correctly with the delegates but perhaps I do not.我确信我已经与代表正确设置了所有内容,但也许我没有。

This is really not a duplicate of Swift Delegate Not Being Called as the solution on that page did not work.这确实不是Swift 未调用代表的副本,因为该页面上的解决方案不起作用。

Any help you could provide would be great.你能提供的任何帮助都会很棒。

Here's the relevant code snippets:以下是相关的代码片段:

My weatherGetterController class:我的weatherGetterController class:

protocol WeatherGetterControllerDelegate {
    func getMyWeather(weather: [FiveDayForecast])
}

class WeatherGetterController: UIViewController, CLLocationManagerDelegate {

var weatherGetterDelegate: WeatherGetterControllerDelegate?

And in the WeatherGetterController "getWeather" function call.并在 WeatherGetterController "getWeather" function 调用中。 The line线

self.weatherGetterDelegate?.getMyWeather(weather: myForecast)

is not being called.没有被调用。

func getWeather() {
       ...
       getNetWeather { (fetchedInfo) in

            if let fetchedInfo2 = fetchedInfo {
                //self.updateUI(mainWeather: fetchedInfo2)
                //need to call delegate here
                let myForecast = self.figureFive(weather: fetchedInfo2)
                //return the forecast
                print(myForecast)
                self.weatherGetterDelegate?.getMyWeather(weather: myForecast)

            }

        }

Finally the function implementation in NoteTakerViewController:最后是 NoteTakerViewController 中的 function 实现:


class NoteTakerViewController: UIViewController, ..., UITextFieldDelegate, WeatherGetterControllerDelegate

    func getMyWeather(weather: [FiveDayForecast]) {

        print("get my weather")
        print(weather)

    }

Through debugging I can see that "weatherGetterDelegate" is set to nil and I don't know why.通过调试我可以看到“weatherGetterDelegate”设置为nil,我不知道为什么。

Thanks谢谢

First you need to make WeatherGetterController a property in NoteTakerViewController or a local variable where you call it but I will use the first option.首先,您需要将 WeatherGetterController 设置为 NoteTakerViewController 中的属性或调用它的局部变量,但我将使用第一个选项。

class NoteTakerViewController: UIViewController, ..., WeatherGetterControllerDelegate {
    var weatherGetterController: WeatherGetterController?

    //...

    func viewDidLoad() {
         //....
         weatherGetterController = WeatherGetterController()
         weatherGetterController?.delegate = self
    }

   //And then in some other part of your code you do
   func someFunc() {
       self.weatherGetterController?.getWeather()

I am curious why WeatherGetterController is define to be a viewcontroller, is that really correct?我很好奇为什么 WeatherGetterController 被定义为视图控制器,这真的正确吗?

class WeatherGetterController: UIViewController, CLLocationManagerDelegate {
       

Personally I would remove that part我个人会删除那部分

class WeatherGetterController: CLLocationManagerDelegate {

Put this in init of WeatherGetterController把它放在 WeatherGetterController 的 init 中

  public weak var weatherGetterDelegate: WeatherGetterControllerDelegate?

  /* if you are not initializing it anywhere else 
  and if you are initializing it anywhere else then make sure you are 
  initializing WeatherGetterController there too and then put delegate   
  of WeatherGetterController to NoteTakerViewController object
   else write the below code */

  var model = NoteTakerViewController()

   public override init() {
      self. weatherGetterDelegate = model
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM