简体   繁体   English

Swift3:Delegate函数未运行

[英]Swift3 : Delegate function is not running

I'm building an app where I start at a TableViewController and when I click on a cell it will go to another viewcontroller and run a function that I created in the new viewcontroller. 我正在构建一个应用程序,我从TableViewController开始,当我单击一个单元格时,它将转到另一个viewcontroller并运行我在新的viewcontroller中创建的函数。 I'm currently using protocols and delegate but even after following along tutorials and guides I find online I am still not able to get the function to run. 我目前正在使用协议和委托,但即使按照教程和指南,我发现在线,我仍然无法运行该功能。

Here's my tableview controller code. 这是我的tableview控制器代码。 I included the code that I thought relevant. 我包含了我认为相关的代码。

class interestPTableViewController: BaseViewController, UITableViewDataSource, UITableViewDelegate {

        var ipSearchDelegate: IPSearch?

        public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {       
                ipSearchDelegate?.ipZoom()
        }
}

Heres my other viewcontroller code. 继承我的其他viewcontroller代码。 I cannot get it to print "hello" 我无法打印“你好”

protocol IPSearch: class{
      func ipZoom()
}

class searchMapViewController: BaseViewController{
      override func viewDidLoad(){
            let ipip = storyboard!.instantiateViewController(withIdentifier: "interestP") as! interestPTableViewController
            ipip.ipSearchDelegate = self
      }
}
extension searchMapViewController: IPSearch {
      func ipZoom(){
           print("hello")
      }
}

Any help will be nice. 任何帮助都会很好。 Thanks in advance. 提前致谢。

Try in this way, you should keep instance of your VC: 试试这种方式,你应该保留VC的实例:

    class searchMapViewController: BaseViewController{
      var ipip: interestPTableViewController! // <- This is important part

      override func viewDidLoad(){
            ipip = storyboard!.instantiateViewController(withIdentifier: "interestP") as! interestPTableViewController
            ipip.ipSearchDelegate = self
      }
}

Your SearchMapViewController's viewDidLoad is instantiating a new InterestPTableViewController and setting its delegate and then discarding it. 您的SearchMapViewController的viewDidLoad正在实例化一个新的InterestPTableViewController并设置其委托然后丢弃它。 That newly created view controller will be deallocated as soon as your viewDidLoad exits. 一旦viewDidLoad退出,新创建的视图控制器将被释放。 The time to set the delegate is in prepareForSegue 设置委托的时间在prepareForSegue中

.

Make your first VC conform to your protocol 使您的第一个VC符合您的协议

 class interestPTableViewController: BaseViewController, UITableViewDataSource, UITableViewDelegate, IPSearch {

        var ipSearchDelegate: IPSearch?

        public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {       
                ipSearchDelegate?.ipZoom()
        }
}

Here is how your task is done in the "conventional" Protocol / Delegate pattern. 以下是您在“常规”协议/代理模式中完成任务的方式。

This is the "map view" controller... 这是“地图视图”控制器......

//
//  SearchMapViewController.swift
//

import UIKit

protocol IPSearch: class{
    func ipZoom()
}

extension SearchMapViewController: IPSearch {
    func ipZoom(){
        print("hello")
    }
}

class SearchMapViewController: BaseViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    // MARK: - Navigation

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

        // assuming another button (hamburger menu?) is triggering a segue
        // to the "table view" controller, this is where you
        // assign the delegate

        if let vc = segue.destination as? InterestPViewController {
            vc.delegate = self
        }

    }

}

This is the View with a Table of rows to select from... 这是带有行表的视图可供选择...

//
//  InterestPViewController.swift
//

import UIKit

class InterestPViewController: BaseViewController, UITableViewDelegate, UITableViewDataSource {

    var delegate: IPSearch?

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    // MARK: - Table view data source

    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 15
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "basicCell", for: indexPath)

        cell.textLabel?.text = "\(indexPath)"

        return cell
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

        // just print so we know we've tapped a row
        print("row selected", indexPath.row)

        // call a func in the "map view" - likely, ipZoom() will be changed to
        // something along the lines of ipZoom(selectedRow: indexPath.row)
        delegate?.ipZoom()

        // returning to the "map view"

        // either
        self.navigationController?.popViewController(animated: true)

        // or
        //presentingViewController?.dismiss(animated: true, completion: nil)

    }

}

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

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