我正在尝试激活 UICollectionView、UIViewController 的 subview 和单独的 ViewController(称为 RegisterController)之间的连接。 CollectionView 都是以编程方式设置的,最后一个单元格上有一个按钮。但是,我不能做典型的 performSegue(_:withIdentfier)
因为按钮在 CollectionView 中。所以,我在 ViewController 中设置了一个方法,将 CollectionView 保存到 performSegue(_:withIdentifier)
。 .我在 CollectionViewController 中设置了一个实例来调用方法:
lazy var registerButton: UIButton = {
let btn = UIButton(type: .system)
btn.setTitle("Don't have an account?", for: .normal)
btn.setTitleColor(lighterOrange, for: .normal)
btn.addTarget(self, action: #selector(registerScreenAppear), for: .touchUpInside)
return btn
}()
@objc func registerScreenAppear(){
let vc: ViewController = ViewController()
_ = vc.toRegister()
}
ViewController中的方法是
func toRegister(){
performSegue(withIdentifier: "toRegister", sender: self)
}
我正在获取调用堆栈:
NSInvalidArgumentException', reason: 'Receiver (<audible.ViewController: 0x7fe780724770>) has no segue with identifier 'toRegister''
我知道我可能正在做一些完全错误的事情,但我搜索了又搜索却找不到解决方案。感谢任何帮助,谢谢
(是的,我已经配置了 segue 并在 Storyboard 中给了它一个标识符,但我必须通过代码执行此操作的原因是因为 CollectionView 和按钮是以编程方式创建的,所以我不能使用 Interface Builder )
最佳答案
在registerScreenAppear方法中调用performSegu
e或像这样调用self.toRegister()
@objc func registerScreenAppear(){
self.toRegister()
}
或
@objc func registerScreenAppear(){
performSegue(withIdentifier: "toRegister", sender: self)
}
注意:使用后面的一个,因为它将删除额外的函数调用,即 toRegister()。
关于ios - Swift 4 - 如何从 UICollectionViewCell 中嵌套的按钮以编程方式激活 segue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48255991/