为presentViewController设置委托(例如不使用segues)

问题描述 投票:0回答:3

我正试图从presentedViewControllerpresentingViewController设置代表。由于我没有使用segues来访问目标ViewController,因此我很难设置目标ViewController的委托。

我希望声明需要在这里发生:

if inspiredActionChosen == "plan" {

        let viewController = storyboard.instantiateViewController(withIdentifier: "planMealNC")


        delegate?.passInspiredRecipeKey(sender: self, inspiredRecipeKey: "-L0ijpon2MDXxPmmw63M")
        print("PASSING")
        self.present(viewController, animated: true, completion: nil)

presentedViewController没有会员代表。除了使用segues之外,还有其他选择吗?

swift delegates uikit
3个回答
1
投票

我对你的问题感到困惑。你想要做的就是将代表传递给前视图控制器,下面的标准做法不起作用?

class PlanMealViewController: UIViewController {

    var presentingDelegate: ThePresentingViewController?

}

class ThePresentingViewController: UIViewController {

    func presentViewController() {

        let viewController = storyboard.instantiateViewController(withIdentifier: "planMealNC")
        viewController.presentingDelegate = self
        present(viewController, animated: true, completion: nil)

    }

}

2
投票

您必须声明委托功能

 protocol presentingProtocol{

  func passInspiredRecipeKey(sender:UIViewController,inspiredRecipeKey:String)

}

带有为协议声明的变量的prsesenting类

class PresentingViewController: UIViewController {

 var presentingDelegate: presentingProtocol?

 }

上课

class presentedViewController: UIViewController {

func presentViewController() {

    let controller = storyboard.instantiateViewController(withIdentifier: "planMealNC") as! PresentingViewController
    controller.presentingDelegate = self
    present(controller, animated: true, completion: nil)

}

}

1
投票

不确定这是否有帮助,找到此链接。不是通过代表,而是添加IBAction在没有segues的故事板之间移动:https://medium.com/@stasost/xcode-a-better-way-to-deal-with-storyboards-8b6a8b504c06

© www.soinside.com 2019 - 2024. All rights reserved.