Domanda

I currently have a UISplitViewController. I have 5 rows in my master view controller and each of the row when tapped will place its respective detail view on screen. It basically works like some sort of menu.

The way I coded it is in the didSelectRow: method.

Here's a sample code

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
        if([indexPath row] == 0)
        {
            firstviewcontroller = [self.storyboard instantiateViewControllerWithIdentifier:@"firstviewcontroller"];

            UINavigationController *nvc = [[UINavigationController alloc] initWithRootViewController:firstviewcontroller];

            NSArray *vcs = [NSArray arrayWithObjects:[self navigationController], nvc, nil];

            [[self splitViewController] setViewControllers:vcs];

            [[self splitViewController] setDelegate:firstviewcontroller];

It's basically the same for the other 4 rows I have.

Right now, for example, if the first row is tapped, it displays the firstviewcontroller. And when the first row is tapped again, it will replace the current instance with a new instance.

How do I prevent this? It's really annoying when I'm filling out the details in the firstviewcontroller (such as textfields, etc) and then I accidentally tap the first row, and it replaces the current instance and all the textfields are empty and need to be filled out again.

I also like to ask how to preserve the viewcontroller instance. For example, I have filled out the data needed in the firstviewcontroller, and then I tap the second row to show the secondviewcontroller. When I tap the first row again, I would like to have the instance of the firstviewcontroller that I already have finished filling the data in. (kind of like how the Settings app works)

È stato utile?

Soluzione

What you should probably do is keep strong references to the content view controller in your menu controller as properties.

@private (nonatomic, strong) UINavigationController *navController1;
@private (nonatomic, strong) UINavigationController *navController2;
....

Then

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if([indexPath row] == 0)
    {
        if (!self.navController1) {
            UIViewController *firstviewcontroller = [self.storyboard instantiateViewControllerWithIdentifier:@"firstviewcontroller"];
            self.navController1 = [[UINavigationController alloc] initWithRootViewController:firstviewcontroller];
        }
        if ([self splitViewController] viewControllers] lastObject] != self.navController1) {
            NSArray *vcs = [NSArray arrayWithObjects:[self navigationController], self.navController1, nil];
            [[self splitViewController] setViewControllers:vcs];
            [[self splitViewController] setDelegate:self.navController1.rootViewController];
        }
    }
}

This way they won't be recreated each time and your state is preserved.

Altri suggerimenti

You can check the class of the currently displayed detail view controller against the class you expect to be there.

if([self.splitViewcontroller.viewControllers.lastObject isKindOfClass:[FirstDetailViewController class]])
{
    //Here you know that the current detail view controller is of the kind of the first master row...
    [self.splitViewcontroller.viewControllers.lastObject.navigationController popToRootViewControllerAnimated:YES]; //You don't have to do this, just an example.
}
else
{
    //Initialize normally
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top