reinitialize root view in iOS

541 views Asked by At

One would think I can easily find an answer to this question, and perhaps I missed it, but here goes.

My app contains a few views of which the main view displays a bunch of information it progressively collects from a user, the mic and and the camera through the other views. It is all supposed to end with one big climactic "submit button." At that point the data gets safely stored (currently in an sql database... but that's another story).

Once that is done, I want the whole process to start over which means reinitializing the view to a virgin state. In android, I can throw a new intent and destroy the old one.

I gather I'm supposed to start with the app delegate (see code below). Now the question is, where do I go from here?

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.mainViewController = [[MainViewController alloc] initWithNibName:@"MainViewController" bundle:nil];
    self.window.rootViewController = self.mainViewController;
    self.mainViewController.managedObjectContext = self.managedObjectContext;

    [self.window makeKeyAndVisible];
    return YES;
}
2

There are 2 answers

0
Rasman On BEST ANSWER

So after a fair share of digging around, and trial and error. I found something that works the way I want it to.

I created a special function in the app delegate which executes the following:

- (void) newScreen
{
    self.mainViewController = [[MainViewController alloc] initWithNibName:@"MainViewController" bundle:nil];
    self.window.rootViewController = self.mainViewController;
    self.mainViewController.managedObjectContext = self.managedObjectContext;
}

Then from main view controller, I call that function

[(AppDelegate *)[[UIApplication sharedApplication] delegate] newScreen];

Easy as pie...

0
Jaybit On

I would create the RootViewController as my starting position. When the rootViewController loads I would initialize and add my First data input process. This way when I hit submit I would call popToRootViewController, and when The rootViewContoller Loads it would initialize and load the first data input process again.