有没有一种方法可以使用委托从其他ViewController退出viewController中的方法?

我有两个viewControllers,还有一个viewController和viewController内部,还有一个viewContainer,它显然也是viewController。

问题是我希望当用户单击viewController(容器视图)中的按钮时,运行一个方法和另一个viewController的方法。

我在viewController(containerView)中有下一个代码

protocol ubicacionContainerViewControllerDelegate {
  func cambiarContainer(posicion : Int)
}

class ubicacionContainerViewController: UIViewController {
var delegate :ubicacionContainerViewControllerDelegate? 

  //i am using tables is for that reason i have this code but when the user select a
  // row i want to run a other method in other viewController

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

    let celda_seleccionada = tableView.cellForRow(at: indexPath) as!
    celdaUbicacionContainerTableViewCell

    ReservaViewController.reserVaServicio.lugar = "\(celda_seleccionada.lbPais) en \ 
   (celda_seleccionada.lbCiudad) "

    delegate?.cambiarContainer(posicion: 5)

}

现在在另一个viewController中,我具有以下代码:

class ReservaViewController: UIViewController,ubicacionContainerViewControllerDelegate {

func cambiarContainer(posicion: Int) {
    //self.posicion = posicion

    if(posicion == 5){
        print(posicion)
    }

}

现在我要对代表另一个viewController的ReservaViewController怎么说,或者我该怎么做?使用委托我运行了它,但是方法cambiarContainer无法运行

tycotyco 回答:有没有一种方法可以使用委托从其他ViewController退出viewController中的方法?

在您的ubicacionContainerViewController中,您可以在didSelect上编写以下代码:

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

let nextView = UIStoryboard.viewController(name: "Storyboard Name",identifier: "Storyboard Identifier") as! ReservaViewController 

nextView.valueInt = 5

self.present(nextView,animated: true,completion: nil)

}

在ReservaViewController中,您可以声明一个变量int

var valueInt = Int!

您可以在其中创建自己的方法:

func setupMethod() {

if valueInt == 5 {

print(valueInt)

} else {

print(valueInt)

}

}
本文链接:https://www.f2er.com/3154733.html

大家都在问