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

时间:2017-12-25 15:37:36

标签: swift delegates uikit

我正在尝试从presentedViewController设置presentingViewController的委托。由于我没有使用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之外,还有其他选择吗?

3 个答案:

答案 0 :(得分: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 :(得分:1)

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

答案 2 :(得分: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)

    }

}