【问题标题】:Delegate function not calling委托函数未调用
【发布时间】:2019-09-20 19:24:50
【问题描述】:

我有 2 个UIViewControllersViewControllerSecondViewController。我在 VC 中定义了委托函数,并在 Second VC 中使用。但是委托函数没有在第二个 VC 中调用。

这是 mu 第一个 VC 代码

import UIKit

//Step1:
protocol testDelegate {
    func testFunction(string1: String, string2:String)
    func math(a:Int, b:Int)
}

class ViewController: UIViewController {

    //Step2:
    var delegateVariable: testDelegate?


override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
}

@IBAction func moveToSecondVC(_ sender: Any) {
    let nav = self.storyboard?.instantiateViewController(withIdentifier: "SVC") as! SecondViewController
    //Step3:
    delegateVariable?.testFunction(string1: "String1", string2: "String2")
    delegateVariable?.math(a:30, b:10)
    self.navigationController?.pushViewController(nav, animated: true)
}

}

我的第二个VC代码

import UIKit

//Step4:
class SecondViewController: UIViewController , testDelegate {

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.

    //Step5:
    let svc = ViewController()
    svc.delegateVariable = self
}

@IBAction func btn(_ sender: Any) {
    //Step5:
    let svc = ViewController()
    svc.delegateVariable = self
}

//Step6:
func testFunction(string1: String, string2: String) {
    print(string1+string2)
}
func math(a:Int, b:Int) {
    print(a+b)
    print(a-b)
    print(a*b)
}
}

这里我只是传递少量数据以供练习,但任何人都可以为我推荐一些高级代表示例教程链接。

【问题讨论】:

    标签: ios swift cocoa-touch delegates protocols


    【解决方案1】:

    这就是为什么什么都没发生...

    let svc = ViewController()
    svc.delegateVariable = self
    

    您正在创建一个新的 ViewController,而不是使用实际使用的那个。

    您似乎没有正确使用委托模式。您的 ViewController 不应调用其他视图控制器上的代码。

    SecondViewController 应该“做一些事情”,然后让 ViewController 知道它做了什么。

    对于 Math 函数,您可以只使用一个新类(不是视图控制器)并根据需要创建和使用它。为此,您不需要 ViewController。

    使用委托的示例可能类似于:

    protocol CreateProfileDelegate: class {
       func didCreateProfile(profile: Profile?)
       func didCancelCreateProfile()
    }
    
    class ViewController: UIViewController {
        func showCreateProfile() {
           let vc = CreateProfileViewController() 
           vc.delegate = self
           present(vc, animated: true)
        }
    }
    
    extension ViewController: CreateProfileDelegate {
    
        func didCreateProfile(profile: Profile?) {
           // show the profile?
        }
    
        func didCancelCreateProfile() {
            // show an alert maybe? 
        }
    }
    

    这样,SecondViewController (CreateProfileViewController) 基本上告诉第一个发生了某些事情,以便它可以对它做出反应。

    【讨论】:

    • 我不明白你的答案,你能用我的代码建议一下吗
    • 抱歉,您的代码没有多大意义。它们不是真实世界的示例(testFunction、数学)您实际上想要做什么?
    • @Scriptable... 委托从 vc1 -> vc2 传递数据需要什么。他不只是使用导航吗?
    • @wings 这就是我在回答中所说的?不需要从 vc1 -> vc2
    • @Scriptable,实际上我正在尝试学习代表,这就是为什么我在最后询问一些好的现实世界示例教程链接。
    【解决方案2】:

    在您正在设置的 SecondViewController 中......

    let svc = ViewController()
    svc.delegateVariable = self
    

    这只是创建ViewController() 类的对象,然后设置委托。所以当 obj.范围的完成,然后对象的内存将自动增加。

    流程应该如下所示......

    在SecondViewController中创建Viewcontroller的对象并设置委托

    let vc = self.storyboard?.instantiateViewController(withIdentifier: "ViewController") as! ViewController
    vc.delegateVariable = self 
    

    然后将视图控制器推入导航堆栈。

    self.navigationController?.pushViewController(svc, animated: true)
    

    在SecondViewController中实现testDelegate的委托方法

     func testFunction(string1: String, string2: String) {
       print(string1+string2)
     } 
    
    func math(a:Int, b:Int) {
    
    }
    

    编辑

    SecondViewController 的最终代码将是……

    import UIKit
    
    
    class SecondViewController: UIViewController , testDelegate {
    
    override func viewDidLoad() {
      super.viewDidLoad()
    
    
     }
    
    @IBAction func btn(_ sender: Any) {
      let vc = self.storyboard?.instantiateViewController(withIdentifier: "ViewController") as! ViewController
      vc.delegateVariable = self 
      self.navigationController?.pushViewController(svc, animated: true)
    }
    
    //MARK:- TestDelegate Methods
     func testFunction(string1: String, string2: String) {
       print(string1+string2)
     }
    
      func math(a:Int, b:Int) {
        print(a+b)
        print(a-b)
        print(a*b)
      }
    }
    

    【讨论】:

    • class SecondViewController: UIViewController , testDelegate { override func viewDidLoad() { super.viewDidLoad() //Step5: let svc = self.storyboard?.instantiateViewController(withIdentifier: "VC") as! ViewController svc.delegateVariable = self } 它没有调用
    • 第一次没有调用
    • 我正在尝试将数据从 vc1 传递到 vc2,这就是为什么我在 vc1 协议 testDelegate { func testFunction(string1: String, string2:String) func math(a:Int, b:Int) }
    • 从这段代码中你需要将数据从vc2传递到vc1,然后delegate就可以工作了
    • @ Mahendra GP,如果我想将数据从 VC1 传递到 VC2 ,我需要做什么? VC2 到 VC2 的情况对我有用,但是 VC1 到 VC2 来传递数据我们需要做什么?
    猜你喜欢
    • 2021-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-29
    • 2012-09-30
    • 1970-01-01
    相关资源
    最近更新 更多