如何在swift 3中从一个视图控制器传递到之前设置委托?

时间:2017-09-08 05:07:46

标签: ios uitableview swift3

这里我试图将所选数据从表格视图传递给上一个视图控制器,但无法执行此操作,下面将提到代码以及在这种情况下设置委托的位置,任何人都可以帮我解决这个问题吗? / p>

protocol ArrayToPass: class {
    func selectedArrayToPass(selectedStrings: [String])
}
class FilterSelectionViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {

var values = [String]()
var delegate: ArrayToPass?

override func viewDidLoad() {
        super.viewDidLoad()
        self.downloadJsonWithURL()
        tableDetails.separatorInset = UIEdgeInsets.zero
        activityIndicator.startAnimating()
        tableDetails.isHidden = true
        tableDetails.dataSource = self
        tableDetails.delegate = self
        let rightBarButton = UIBarButtonItem(title: "Apply", style: UIBarButtonItemStyle.plain, target: self, action: #selector(applyBarButtonActionTapped(_:)))
        self.navigationItem.rightBarButtonItem = rightBarButton
        tableDetails.estimatedRowHeight = UITableViewAutomaticDimension
        tableDetails.rowHeight = 60
        // Do any additional setup after loading the view.
    }
    func applyBarButtonActionTapped(_ sender:UIBarButtonItem!){
        let vc = FilterSelectionViewController()
        vc.delegate = self as! ArrayToPass
        navigationController?.popViewController(animated: true)
    }

 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){
        selected = false
        if let cell = tableView.cellForRow(at: indexPath as IndexPath) {
            if cell.accessoryType == .checkmark{
                cell.accessoryType = .none
                print("\(productName[indexPath.row])")
                values = values.filter{$0 != "\(productName[indexPath.row])"}
                selected = true
            }
            else{
                cell.accessoryType = .checkmark
            }
        }
        if selected == true{
            print(values)
        }
        else{
            getAllTextFromTableView()
        }
        self.delegate?.selectedArrayToPass(selectedStrings: values)
        print(values)
    }
    func getAllTextFromTableView() {
    guard let indexPaths = self.tableDetails.indexPathsForSelectedRows else { // if no selected cells just return
    return
    }

    for indexPath in indexPaths {
        values.append(productName[indexPath.row])
    }
    }

在这个视图控制器中我试图获取数据

class FilterViewController: UIViewController,UITableViewDataSource,UITableViewDelegate,ArrayToPass {

 var values: [String] = []

 func selectedArrayToPass(selectedStrings: [String]){
        values = selectedStrings
    }

3 个答案:

答案 0 :(得分:1)

在进入第二个视图之前,您需要确认协议

class FilterViewController: UIViewController,UITableViewDataSource,UITableViewDelegate,ArrayToPass {

 var values: [String] = []

 func actionGotoFilterSection(){
  // class initilization with navigation 
     let filterSection = self.storyBoard.instantiateViewController(withIdentifier: "filterSectionIdentifier") as! FilterSelectionViewController
    filterSection.delegate = self 
    self.navigationController?.pushViewController(filterSection, animated: true)
 }    

 //MARK: - Delegate Methods 
 func selectedArrayToPass(selectedStrings: [String]){
        values = selectedStrings
    }
}

答案 1 :(得分:1)

在推送或呈现viewController(FilterSelectionViewController)之前 您需要将delegate分配给self或任何NSObject类,否则代理将不会调用

这是示例

@IBAction func btnNextTapped(_ sender: UIButton) {
  let vc: FilterSelectionViewController  =  // Assign VC
  vc.delegate = self  //MISSING PART
  // PUSH OR PRESENT VC
}

答案 2 :(得分:0)

在FilterSelectionViewController中重定向时,你必须像这样设置delegate = self

let main = UIStoryboard(name: "Main", bundle: nil)
let vc = main.instantiateViewController(withIdentifier: "tempVC") as! tempVC// Your FilterSelectionViewController
vc.delegateColor = self //your delegate
navigationController?.pushViewController(vc, animated: true)