I talked earlier this week about these and have done further work. The problem I was solving was this: The App will be installed from the App Store but is only for a subset of users. It would be very bad if anyone else was able to use it.
So When it’s first run, it looks for Config info. As it’s never been run and this config info is set internally, it comes up with a screen asking for a few fields to be filled in, but only saves the data if a master password is entered. This information is saved out (password fields are used) and then the App kills the main View, recreates it ie effectively it restarts and upon finding the correct information it works properly. This was done in the View’s ViewDidLoad.
What I found was that the ViewWillAppear only appears to fire the first time and not the second following the View being disposed (UiViews are disposable) and then recreated in the a method in ViewController.
One of the nice things about Visual Studio 2012 is the Code Analysis. It’s worth doing to try and find underlying problems and I found some in mine. The code worked fine, but Code Analysis told me I was doing naughties with a Virtual Method in a constructor.
Mea culpa. I was indeed. I’d created a View object with my Subclassed View in a CustomViewController.
public override void ViewDidLoad()
View = new MainView(UIScreen.MainScreen.Bounds);
But I’d been doing all sorts in the MainView() constructor that needed not to be there, though it had seemed to run ok. If configuration data is absent I want to stop things and have it entered. So I was creating SubViews (Controls) and then calling the UIView.AddSubView() that MainView inherited from within the Constructor. Shock Horror Probe! Bad Code Alert…
The risk is that a virtual method may not have been correctly assigned and the compiler doesn’t really like Constructors in C# to have to worry about this. After a Constructor has finished, things should be just swell.
The trick is to move the code into a subclassed ViewController.ViewWillAppear(). This is called whenever the View is about to appear. Use a bool variable to let things that should be initialized here just the once be only called just the once.
public override void ViewWillAppear(bool animated)
if ((View as MainView).ConfigCheck() && OneOff)
OneOff = false;
// one off code here