簡體   English   中英

委托函數未調用

[英]Delegate function not calling

我有2個UIViewControllersViewControllerSecondViewController 我在VC中定義了委托函數,並在Second VC中使用了它。 但是委托函數不調用Second VC。

這是畝首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)
}
}

在這里,我只是傳遞少量數據進行練習,但是任何人都可以為我提出一些高級委托示例教程鏈接。

這就是為什么什么都沒有發生的原因...

let svc = ViewController()
svc.delegateVariable = self

您正在創建一個新的ViewController,而不使用實際使用的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)基本上會告訴第一個發生了什么事情,以便它可以做出反應。

在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)
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM