Setting UILabel on different ViewController in same ViewController.swift

Hello,



I am trying to create an iOS app with Swift in Xcode however when I try to set a label in a different view controller from the same Swift class the app just crashes.



Any help would be appreciated,



Thanks.


Code:


@IBOutlet weak var startButton: UIButton!
    @IBOutlet var secondViewController: UIView!
    @IBOutlet weak var welcomeBack: UILabel!
    @IBOutlet weak var start: UILabel!
    
    
   
    
    @IBAction func nameReset(_ sender: Any) {
        
        DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(1), execute: {
            let alertController2 = UIAlertController(title: "Name reset.", message: "Your name has been reset.", preferredStyle: UIAlertControllerStyle.alert)
            
            alertController2.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: { action in
                
                
            }))
            
            alertController2.show()
            
            self.defaults.set(false, forKey: "customerNameSetUp")
        })
        
    }
    
    func welcomeBackText() {
        let welcomeLabel = "Welcome new user!"
        start.text = welcomeLabel
    }
    
    @IBAction func onStart(_ sender: Any) {
        
        if(!self.defaults.bool(forKey: "customerNameSetUp")) {
            
            welcomeBackText()
                
            DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(1), execute: {
                
                let alertController = UIAlertController(title: "What is your name?", message: "We need your name to accommodate the app for you.", preferredStyle: UIAlertControllerStyle.alert)
                
                alertController.addTextField { (textField) in
                    textField.placeholder = "Enter your name"
                    textField.clearButtonMode = .whileEditing
                }
                
                alertController.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: { action in
                    
                    
                    
                    func customerNameAlert() {
                        let customerName = alertController.textFields![0] as UITextField
                        self.defaults.set(customerName.text, forKey: "customerInfoName")
                        let whitespaceSet = CharacterSet.whitespaces
                        if (customerName.text?.trimmingCharacters(in: whitespaceSet).isEmpty)! {
                            let failName = UIAlertController(title: "Error!", message: "Your name can not be empty!", preferredStyle: UIAlertControllerStyle.alert)
                            
                            failName.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: { action in
                                
                                alertController.show()
                            }))
                            
                            failName.show()
                        } else {
                            let success = UIAlertController(title: "Welcome!", message: self.defaults.string(forKey: "customerInfoName"), preferredStyle: UIAlertControllerStyle.alert)
                            
                            success.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: { action in
                                
                            self.welcomeBack.text = String("Welcome back:" + customerName.text!)
                            
                            }))
                            
                            success.show()
                            
                        }
                        self.defaults.set(true, forKey: "customerNameSetUp")
                    }
                    
                    customerNameAlert()
                    
                }))
                
                
                
                alertController.show()
                
            })


        }
        
    }
    
       override func viewDidLoad() {
        super.viewDidLoad()


    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }




}
public extension UIAlertController {
    func show() {
        let win = UIWindow(frame: UIScreen.main.bounds)
        let vc = UIViewController()
        vc.view.backgroundColor = .clear
        win.rootViewController = vc
        win.windowLevel = UIWindowLevelAlert + 1
        win.makeKeyAndVisible()
        vc.present(self, animated: true, completion: nil)
    }
}

Replies

Please clarify in which line you get the crash, and how you get all relevant values.


You say a different view controller , but your code shown does not include any code about another instance of view controller.


All such things may affect, please provide enough info.

Notifications can be an esay way to do this.

Hello thank you for your responces but as OOPer said "Please clarify in which line you get the crash, and how you get all relevant values."


so I am going to clarify.


https://ibb.co/eWStFJ


^^ There is an image of my view controllers and navigation controller.


And line: 35 is where the crash occurs and it is declaired in: lines 26, 29.


I hope this helps,


Thanks.