简体   繁体   English

快速从模态表视图返回值到主表视图(IOS)

[英]Returning a value from a modal tableview to the main tableview in swift (IOS)

I have a main viewController with a tableview. 我有一个带表视图的主viewController。 When a button is pressed on a cell, a modal will appear and allow user to select multiple selection. 当在单元格上按下按钮时,将出现一个模态,并允许用户选择多个选择。 So I presented a modal controller which is again a tableViewController which on dismiss will give a string value. 因此,我提出了一个模态控制器,它又是一个tableViewController,在关闭时将给出一个字符串值。 But once modalView is dismissed and returns to my main tableView, tableView.reload() shows Nil while unwrapping an optional value. 但是一旦modalView被关闭并返回到我的主tableView,tableView.reload()在展开一个可选值时将显示Nil。

Code : 代码:

// In modalTableViewController
//Protocol
protocol modalViewControllerDelegate {
func sendText( text: String)
}

//after all cell operations Done Button Action
@IBAction func doneBtnPressed(_ sender: Any) {

    mystr = selectedValues.joined(separator: ",")
    if(mystr != "")
    {
        let vc = viewController()
        delegate?.sendText(text: self.mystr)
    }
    self.dismiss(animated: true, completion: nil)
}
//In viewController
class ViewController : UIViewController,modalViewControllerDelegate{
 func sendText(text: String) {
    saveFormValue(mystr: text)
}
// .....
//SaveFunction
func saveFormValue(mystr:String){
self.tableView.reloadData() //HERE IAM GETTING ERROR (FOUND NIL)
//save operations goes here

}


.....
///
//Checking Cell type and calling function
if (cell.type == .multiSelect) {
    showCheckBox(data: ["One","Two","Three"])       
}
// Modal view function
func showCheckBox(data:[String])
{
    print("ShowCheckBox")
    checkBoxData = data //Assigning to a globalVariable
    self.performSegue(withIdentifier: "multiselectSegue", sender: self)
}

Are you allocating the mainVC as the modal's delegate? 您是否将mainVC分配为模式的委托? I believe that is the step you are missing (based on the code posted) 我相信这是您所缺少的步骤(基于发布的代码)

If you are, you might want to add breakpoints and check that the sendText and saveFormValue methods are actually getting called. 如果是这样,则可能要添加断点并检查sendTextsaveFormValue方法是否真正被调用。

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

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