OGeek|极客世界-中国程序员成长平台

标题: ios - 如何关闭 ORKTaskViewController 并呈现我选择的 View Controller ? [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 18:32
标题: ios - 如何关闭 ORKTaskViewController 并呈现我选择的 View Controller ?

我花了 24 小时试图找到解决此问题的方法。当用户在我的应用程序上点击注册时,他们必须回答一系列调查问题(我使用 ORKorderedtask(研究工具包)创建的)。完成调查后,我希望显示主页,但是当我测试应用程序并完成调查时,它会直接返回注册页面。这是我的代码:

1.呈现有序的任务 View Controller ;

let registrationTaskViewController = ORKTaskViewController(task:  registrationSurvey, taskRun: nil)
registrationTaskViewController.delegate = self
self.present(registrationTaskViewController, animated: true, completion: nil)

<强>2。关闭任务 View Controller (这不起作用);

func taskViewController(_ taskViewController: ORKTaskViewController, didFinishWith reason: ORKTaskViewControllerFinishReason, error: Error?) {
    self.dismiss(animated: false) {
    let home = homePageViewController()
    self.present(home, animated: true, completion: nil)
}

提前致谢。



Best Answer-推荐答案


如果不知道堆栈中的所有 ViewController,我建议不要关闭您的注册页面 View Controller 。而是在您的注册屏幕顶部显示您的 HomePageViewController。只需将您的委托(delegate)方法更改为:

func taskViewController(_ taskViewController: ORKTaskViewController, didFinishWith reason: ORKTaskViewControllerFinishReason, error: Error?) {
    let home = homePageViewController()
    self.present(home, animated: true, completion: nil)
}

或者你甚至可以在你提交你的 ORKTaskViewController 之后在完成 block 中显示你的 HomePageViewController。这种方法的好处是,当用户关闭调查时,他们会立即看到 HomePageViewController:

let registrationTaskViewController = ORKTaskViewController(task:  registrationSurvey, taskRun: nil)
registrationTaskViewController.delegate = self
self.present(registrationTaskViewController, animated: true, completion: {
    let home = homePageViewController()
    self.present(home, animated: true, completion: nil)
})

还有几点:

• 类应以大写字母开头(即 HomePageViewController)。这是每个有经验的开发人员都使用的约定,Apple 甚至推荐。

• 最后,我建议使用导航 Controller 来处理这些转换。使用导航 Controller ,您可以使用推送 segues 实现更好的“流程”。只是感觉好多了。

关于ios - 如何关闭 ORKTaskViewController 并呈现我选择的 View Controller ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45827130/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4