[英]how to add a button that overrides a storyboard segue (in swift 3)
我添加了一个分镜脚本segue来向照片添加文本。 现在,您将必须通过3个视图控制器才能获得最终照片。 A(选项菜单)-> B(输入文字)-> C(拍照)。 如果我向vc b添加一个按钮,它将显示为sigabrt错误。 如果我注释掉下面的代码,则没有错误。 显而易见的问题是分镜脚本segue。 我想要做的就是有一个按钮,它可以从b-> a转到,而不会出现运行时错误。 下面的代码是vc b。 只是让您知道我可以从c开始-> a没问题,但我想从b-> a开始
import UIKit
class customViewController: UIViewController {
@IBOutlet weak var zext: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
var destviewc: ccViewController = segue.destination as! ccViewController
destviewc.labelText = zext.text!
} }
您的prepare(for:sender:)
正在对某些值的存在进行运行时假设(使用!
)。 即, segue.destination
是ccViewController
而zext.text
不是nil。 如果这些假设不正确,您将遇到运行时错误。
相反,您可以使用Swift guard
语句使用一些防御技术,以确保您的方法处于正确的状态以继续执行。
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
guard let destViewC = segue.destination as? CCViewController else {
return
}
guard let text = zext.text else {
return
}
destViewC.labelText = text
}
另外,类型名称应使用大写字母大写。 也就是说,“ customViewController”应为“ CustomViewController”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.