关闭并显示按钮上的视图单击另一个视图

时间:2017-03-13 17:01:55

标签: ios swift3

我有一个容器视图,里面有一个表视图。当我在表视图上选择一个单元格时,它会显示一个视图控制器(1)。该视图控制器中有一个按钮,它呈现另一个视图控制器(2)。当我离开2时,我需要1来重新加载它的数据。我假设这样做,我需要在离开后再解雇并再次出示1.我怎样才能做到这一点?

用于呈现VC2的VC1代码:

    @IBAction func contributeButtonTapped(_ sender: Any) {
    if totalContributions >= NeedStore.shared.currentNeed!.amount {
        let alert = UIAlertController(title: "", message: "This need has already been fully contributed to.", preferredStyle: .alert)
        let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default)
        alert.addAction(okAction)
        self.present(alert, animated: true, completion: nil)
    } else {
        let storyboard = UIStoryboard(name: "Contribute", bundle: Bundle.main)
        let viewController = storyboard.instantiateViewController(withIdentifier: "ContributeViewController") as! ContributeViewController
        viewController.view.backgroundColor = .clear
        viewController.modalPresentationStyle = .overCurrentContext
        self.present(viewController, animated: false, completion: nil)
    }
}

用于切换回VC1的VC2代码:

    func detailView() {
    ApiManager.shared.getNeed(needId: NeedStore.shared.currentNeed!.id) { (need) in
        NeedStore.shared.currentNeed = need
        ApiManager.shared.getNeedUser { (object) in
            if object {
                self.dismiss(animated: false, completion: nil)
                let storyboard = UIStoryboard(name: "NeedDetail", bundle: Bundle.main)
                let viewController = storyboard.instantiateViewController(withIdentifier: "NeedDetailViewController") as! NeedDetailViewController
                viewController.view.backgroundColor = .clear
                viewController.modalPresentationStyle = .overCurrentContext
                self.present(viewController, animated: false, completion: nil)
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

在VC {1}的viewDidAppear()中添加self.tableview.reloadData()

如果这不起作用,您可能需要创建一个委托,因此它知道您要离开VC(2),输入(1)并刷新它。

更新 -

添加了委托方法

protocol: ViewController2Delegate: class {
 func reloadTableView()
}

class ViewController2: UIViewController {


weak var delegate: ViewController2Delegate?

@IBOutlet func goBackButton () {
 delegate.reloadTableView()
 }
}

VC1 -

class ViewController1: UIViewController, ViewController2Delegate {
 func reloadTableView() {
   self.tableView.reloadData()
   *- Whatever code you might be using to call your data too-*
 }

}