我是 Xcode 的新手,不知道为什么会出现这个错误,有人可以帮我解决这个问题吗?非常感激。这基本上是为了让用户输入给定的验证码,它会根据特定代码显示一条消息。
验证 Controller
import UIKit
class VerificationController: UIViewController {
@IBOutlet var verification: UITextField!
@IBAction func enter(_ sender: Any) {
if verification.text != ""
{
if verification.text == "123"
{
performSegue(withIdentifier: "segue", sender: self)
}
else if verification.text == "234"
{
performSegue(withIdentifier: "segue", sender: self)
}
}
}
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.
}
}
第二 Controller
import UIKit
class SecondViewController: UIViewController {
@IBOutlet var label: UILabel!
var myString1 = "Hello 123"
var myString2 = "Hello 234"
override func viewDidLoad() {
super.viewDidLoad()
if (VerificationController().verification.text! == "123")
{
label.text = myString1
}
else if (VerificationController().verification.text! == "234")
{
label.text = myString2
}
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
最佳答案
您需要将验证文本传递给prepare(forSegue) 方法中的SecondViewController
并访问它:
在 VerificationController
中:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "segue" {
let secondVC = segue.destination as! SecondViewController
secondVC.verificationText = verification.text
}
}
然后在 SecondViewController
中:
var verificationText:String?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
if (verificationText == "123")
{
label.text = myString1
}
else if (verificationText == "234")
{
label.text = myString1
}
}
希望对您有所帮助!
关于ios - 线程 1 : EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP) Error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45831526/