ios - how to set delegate while passing from one view controller to previous in swift 3? -
here trying pass selected data table view previous view controller unable , code mentioned below , set delegate in case can me how resolve ?
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 // additional setup after loading 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 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 return return } indexpath in indexpaths { values.append(productname[indexpath.row]) } }
in view controller trying data
class filterviewcontroller: uiviewcontroller,uitableviewdatasource,uitableviewdelegate,arraytopass { var values: [string] = [] func selectedarraytopass(selectedstrings: [string]){ values = selectedstrings }
at time of redirection in filterselectionviewcontroller have set delegate = self this
let main = uistoryboard(name: "main", bundle: nil) let vc = main.instantiateviewcontroller(withidentifier: "tempvc") as! tempvc// filterselectionviewcontroller vc.delegatecolor = self //your delegate navigationcontroller?.pushviewcontroller(vc, animated: true)
wiki
Comments
Post a Comment