以编程方式执行 segue
Perform a segue programmatically
您好,我正在尝试在没有 Storyboard 的情况下以编程方式执行 segue。目前我有这个作为我的代码:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "popOutNoteExpanded" {
let vc = segue.destination as! ExpandedCellViewController
let cell = sender as! AnnotatedPhotoCell
sourceCell = cell
vc.picture = cell.imageView.image
print("button pressed")
}
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
performSegue(withIdentifier: "popOutNoteExpanded", sender: indexPath)
}
当我点击集合视图单元格时,它说:
has no segue with identifier 'popOutNoteExpanded'.
不确定如何执行我的自定义动画过渡。
转场是故事板的组成部分。如果您没有故事板,则无法执行转场。但是您可以像这样使用当前视图控制器:
let vc = ViewController() //your view controller
self.present(vc, animated: true, completion: nil)
要以编程方式触发 segue,您必须执行以下步骤:
1. 通过 在两个视图控制器之间拖动你想要的 segue 和 设置它的标识符 来在 Storyboard 中设置它(例如,在你的情况下是 "popOutNoteExpanded") 在属性检查器部分。
2.以编程方式调用它
performSegue(withIdentifier: "popOutNoteExpanded", sender: cell)
请检查您是否正确设置了它的标识符。
此外,在您上面的代码中,您输入了一个错误的发件人。
在您的 prepare() 方法中,您将发件人用作 UITableViewCell,但您调用 performSegue() 发件人作为 IndexPath.
您需要一个单元格,请致电:
let cell = tableView.cellForRow(at: indexPath)
然后你可以执行一个segue:
performSegue(withIdentifier: "popOutNoteExpanded", sender: cell)
除了正确答案之外,请注意,如果您在导航堆栈中(例如,您在“设置”页面中并且想要转到“我的帐户”VC),您应该使用:
let vc = MyAccountVC()
self.navigationController?.pushViewController(vc, animated: true)
如果导航VC在转换过程中出现透明背景,只需将其backgoundColor
设置为.white
。
在我的例子中,我展示了一个 TableViewController,没有定义 segue,并且发现有必要从情节提要中实例化,否则 tableViewCells
无法在 cellForRowAt
中出列。
let vc = (storyboard.instantiateViewController(withIdentifier: "YourTableViewController") as? YourTableViewController)!
self.navigationController!.pushViewController( vc, animated: true)
您好,我正在尝试在没有 Storyboard 的情况下以编程方式执行 segue。目前我有这个作为我的代码:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "popOutNoteExpanded" {
let vc = segue.destination as! ExpandedCellViewController
let cell = sender as! AnnotatedPhotoCell
sourceCell = cell
vc.picture = cell.imageView.image
print("button pressed")
}
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
performSegue(withIdentifier: "popOutNoteExpanded", sender: indexPath)
}
当我点击集合视图单元格时,它说:
has no segue with identifier 'popOutNoteExpanded'.
不确定如何执行我的自定义动画过渡。
转场是故事板的组成部分。如果您没有故事板,则无法执行转场。但是您可以像这样使用当前视图控制器:
let vc = ViewController() //your view controller
self.present(vc, animated: true, completion: nil)
要以编程方式触发 segue,您必须执行以下步骤:
1. 通过 在两个视图控制器之间拖动你想要的 segue 和 设置它的标识符 来在 Storyboard 中设置它(例如,在你的情况下是 "popOutNoteExpanded") 在属性检查器部分。
2.以编程方式调用它
performSegue(withIdentifier: "popOutNoteExpanded", sender: cell)
请检查您是否正确设置了它的标识符。
此外,在您上面的代码中,您输入了一个错误的发件人。
在您的 prepare() 方法中,您将发件人用作 UITableViewCell,但您调用 performSegue() 发件人作为 IndexPath.
您需要一个单元格,请致电:
let cell = tableView.cellForRow(at: indexPath)
然后你可以执行一个segue:
performSegue(withIdentifier: "popOutNoteExpanded", sender: cell)
除了正确答案之外,请注意,如果您在导航堆栈中(例如,您在“设置”页面中并且想要转到“我的帐户”VC),您应该使用:
let vc = MyAccountVC()
self.navigationController?.pushViewController(vc, animated: true)
如果导航VC在转换过程中出现透明背景,只需将其backgoundColor
设置为.white
。
在我的例子中,我展示了一个 TableViewController,没有定义 segue,并且发现有必要从情节提要中实例化,否则 tableViewCells
无法在 cellForRowAt
中出列。
let vc = (storyboard.instantiateViewController(withIdentifier: "YourTableViewController") as? YourTableViewController)!
self.navigationController!.pushViewController( vc, animated: true)