2017-08-23 55 views
1

我是Xcode和idk的新手爲什麼我有這個錯誤,有人可以幫助我嗎?非常感激。這基本上是讓用戶輸入他們給出的驗證碼,它會根據特定的代碼顯示一條消息。線程1:EXC_BAD_INSTRUCTION(code = EXC_I386_INVOP)錯誤

VerificationController

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. 
    } 

} 

SecondController

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. 
    } 
} 

[IMAGE OF ERROR]

+0

您正在創建「VerificationController()」的新對象,並提供擔保verification.text永遠不會爲零,並檢查它是否等於「123」。您是否在創建「VerificationController()」對象的計時器上爲verification.text賦值? –

回答

2

您需要通過驗證文本到SecondViewController準備(forSegue)方法和訪問相同的:

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 
    } 

} 

希望它能幫助!

+1

我真的希望我能,但我沒有足夠的代表點:(如果不是我會upvote 100x – Kinja

+0

@Kinja Np,謝謝。 –

+0

@Kinja而不是改變你的問題的標題,以包括「已解決」,如果這個答案解決你的問題,你應該通過點擊左邊的複選標記來接受答案。 – rmaddy

相關問題