0

I'm just learning XCode and now I have the following problem, I've tried a few things but I can't get any further at this point, does anyone have an idea of what this could be. I have two ViewControllers. With the first ViewController you have the possibility to open a popup for user input via a button (second ViewControler). After entering the user, you can save the user with a save button and you should get back to the main page. Then I always get the error

"Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[PwGen.ViewControllerUser tfUser:]: unrecognized selector sent to instance "

///////////////////////////Button save
@IBAction func btnConfirm(_ sender: UIButton)
{
    save()

    let story = UIStoryboard(name: "Main", bundle: nil)
    let controller = story.instantiateViewController(withIdentifier: "ViewController") as! ViewController
    controller.modalPresentationStyle = .fullScreen
    self.present(controller, animated: false, completion: nil)
}

///////////////////////////function  save
func save()
{
    let strEingabe = tfUser.text
    let defaults = UserDefaults.standard
    defaults.set(strEingabe, forKey: "customTextUser")
    defaults.synchronize()
    let strtest = defaults.string(forKey: "customTextUser")
    print(strtest)
}

Thanks a lot

Hi guys,

I've tried a few more things, but without success Here is the code of my second ViewControll. If I don't enter anything in the "tfUser" text field, everything works as desired and the first view controller is opened and the code of the first view is processed. However, if you enter something in "tfUser", the value is saved, the first ViewController is opened and then crashes, although the same code is processed as if you left "tfUser" empty i don't know why its working when "tfUser" is empty to and string in "tfUser".

import UIKit

class ViewControllerUser: UIViewController {

@IBOutlet weak var tfUser: UITextField!

override func viewDidLoad()
{
    super.viewDidLoad()

    // Do any additional setup after loading the view.
}

//Button abort
@IBAction func btnBack(_ sender: UIButton)
{
    tfUser.text = ""
    let vc = storyboard?.instantiateViewController(withIdentifier: "ViewController") as! ViewController
    vc.modalPresentationStyle = .fullScreen
    present(vc, animated: false)
}

//Button confirm
@IBAction func btnConfirm(_ sender: UIButton)
{
    save()
    let vc = storyboard?.instantiateViewController(withIdentifier: "ViewController") as! ViewController
    vc.modalPresentationStyle = .fullScreen
    present(vc, animated: false)
}

//function  save
func save()
{
    let strEingabe = ("\(tfUser.text ?? "")")
    let defaults = UserDefaults.standard
    defaults.set(strEingabe, forKey: "customTextUser")
    tfUser.text = ""
}

}

Joakim Danielson
  • 43,251
  • 5
  • 22
  • 52
greddy2010
  • 13
  • 2
  • it is telling you that there is no 'tfUser' method in the PwGen.ViewControllerUser – Teja Nandamuri Aug 31 '22 at 14:45
  • Unrelated but don't use `synchronize` and reading the value back right after writing it is pointless. – vadian Aug 31 '22 at 14:55
  • 1
    The exception is not from the code in the question. To find the source of the exception, add an exception breakpoint as described here: https://stackoverflow.com/a/17802723/1176162 – Yonat Aug 31 '22 at 16:04
  • I tried it with the exception breakpoint but without success, I add in my question the complete code of my ViewControllerUser – greddy2010 Sep 11 '22 at 09:27

0 Answers0