如何在代码中覆盖segue的种类?

时间:2016-06-17 07:04:22

标签: ios swift uistoryboardsegue

我只需要从FirstViewControllerSecondViewController有一段显示。 我在FirstViewController的两个地方用iPad和iPhone拨打电话。没关系,但在一种情况下,我需要将SecondViewController作为.Popover

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    if let secondViewController = segue.destinationViewController as? SecondViewController {

        if let cell = sender as? UITableViewCell, indexPath = tableView.indexPathForCell(cell) {

            let student = fetchedResultsController.objectAtIndexPath(indexPath) as! Student
            secondViewController.schoolClass = schoolClass
            secondViewController.student = student

        } else {

            secondViewController.modalPresentationStyle = .Popover
            secondViewController.schoolClass = schoolClass

            let popoverPresentationController = studentFormViewController.popoverPresentationController
            popoverPresentationController!.delegate = self
            popoverPresentationController?.permittedArrowDirections = .Up
            popoverPresentationController?.barButtonItem = addStudentBarButtonItem
        }
    }
}

它不起作用。

如何在代码中覆盖故事板中的segue类型?

1 个答案:

答案 0 :(得分:-1)

您应该使用自定义segues:

  1. 通过继承UIStoryboardSegue类并覆盖prepare()方法来实现自定义segue。 (在prepare()方法中,您可以实现所有必需的行为(控制器动画,演示等,具体取决于您的情况);
  2. 在界面构建器中将segue类型从push更改为custom,并在class属性中选择您的实现(如下图所示)
  3. Using custom segue

    祝你好运!