2017-08-23 58 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

相关问题