简体   繁体   English

如何添加一个覆盖故事板序列的按钮(快速3)

[英]how to add a button that overrides a storyboard segue (in swift 3)

I added a storyboard segue to add text to a photo. 我添加了一个分镜脚本segue来向照片添加文本。 Right now you would have to go through 3 view controllers to get the final photo. 现在,您将必须通过3个视图控制器才能获得最终照片。 A(options menu) -> B(enter text) -> C(take photo). A(选项菜单)-> B(输入文字)-> C(拍照)。 If I add a button to vc b it shows up as a sigabrt error. 如果我向vc b添加一个按钮,它将显示为sigabrt错误。 If I comment out the code below there is no error. 如果我注释掉下面的代码,则没有错误。 Its obvious the issue is the storyboard segue. 显而易见的问题是分镜脚本segue。 All i want to do is have a button that can go from b->a without a runtime error coming up. 我想要做的就是有一个按钮,它可以从b-> a转到,而不会出现运行时错误。 The code below is vc b. 下面的代码是vc b。 Just to let you know I can go from c ->a no problem but I want to go from b -> a 只是让您知道我可以从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!


} }

Your implementation of prepare(for:sender:) is making runtime assumptions about the presence of certain values (using ! ). 您的prepare(for:sender:)正在对某些值的存在进行运行时假设(使用! )。 Namely, that segue.destination is a ccViewController and zext.text is not nil. 即, segue.destinationccViewControllerzext.text不是nil。 If these assumptions are incorrect you will experience a runtime error. 如果这些假设不正确,您将遇到运行时错误。

Instead you can employ some defensive techniques using the Swift guard statements to ensure your method is in the right state to continue execution. 相反,您可以使用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
}

Also, type names should use upper camel case. 另外,类型名称应使用大写字母大写。 That is, "customViewController" should be "CustomViewController". 也就是说,“ customViewController”应为“ CustomViewController”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM