I am new to iOs app development and I wonder if my app architecture is correct. A bug in my app make me believe that some of my variables are initialized several times.
To summarize my app : 2 screens and 2 views but only one controller. Depending the current view, the viewDidLoad have different results. I don't believe this is the right way to do. I guess the idea would be to create a controller for each view ?
But my main concern here : in my viewDidLoad, when the main screen is loaded, I set up a notification observer. I believe( because of bugs ) that this observer is setup each time the screen load and then is called multiple times.
My questions here : Where to put this listener , is there a place that will run the code only once this view is loaded ? It should be fixed by putting this listener into a variable ?
Is the AppDelegate application function a right place for that kind of things ?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
// Remove iphone sleep mode
UIApplication.shared.isIdleTimerDisabled = true
//Setup the external scanner object
self.scanner.delegate = self
self.scanner.connect()
// Init the saved values
let defaults = UserDefaults.standard
// --------------- MAIN VIEW ---------------
if(mainView != nil){
// Add a notification receiver
// Will receive results ### request
NotificationCenter.default.addObserver(self, selector: #selector(mainTextNewLineNotification), name: Notification.Name(rawValue: "sendingToView"), object: nil)
// Layout setup
mainTextView.layer.cornerRadius = 6.0
[...]
}
// --------------- SETTINGS VIEW ---------------
if(settingsView != nil){
//Fill the field with saved values
inputHost.text = defaults.string(forKey: "hostname")
inputPort.text = String(defaults.integer(forKey: "port"))
if(defaults.string(forKey: "timeout") != nil){
inputTimeout.text = defaults.string(forKey: "timeout")
}
if(UserDefaults().string(forKey: "confirmSwitch") == "On"){
confirmSwitch.isOn = true
} else {
confirmSwitch.isOn = false
}
}
}