[英]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.