Three20's URL-based navigation + tab bar examp

2019-05-06 13:27发布

问题:

I just found out how easy it is (or at least is supposed to be) to do state persistence with Three20 library. However, I am unable to figure out how to use the URL mapper with a tab bar (UITabBarController).

The situation is this:

  1. I have four tabs and different controllers for them: FirstViewController, SecondViewController, ThirdViewController and FourthViewController.
  2. I want to map those to tt://tabs/first, ..., tt://tabs/fourth respectively, and have them saved somewhere when application closes so that the previously viewed tab is automatically opened when the application starts again.

My code so far:

// Init the tab bar
tabBarController = [[UITabBarController alloc] init];
[tabBarController setDelegate:self];

// Init the navigator
TTNavigator *navigator = [TTNavigator navigator];
[navigator setWindow:window];
[navigator setPersistenceMode:TTNavigatorPersistenceModeAll];

// Begin mapping
TTURLMap *map = [navigator URLMap];
[map from:@"tt://tabs" toViewController:[UIViewController class]];
[map from:@"tt://tabs/first" toViewController:[FirstViewController class]];
[map from:@"tt://tabs/second" toViewController:[SecondViewController class]];
[map from:@"tt://tabs/third" toViewController:[ThirdViewController class]];
[map from:@"tt://tabs/fourth" toViewController:[FourthViewController class]];

// Try restoring
if (! [navigator restoreViewControllers]) {
  // Open default
  TTURLAction *defaultAction = [[TTURLAction alloc] initWithURLPath:@"tt://tabs/default"];
  [defaultAction setParentURLPath:@"tt://tabs"];
  [navigator openURLAction:defaultAction];
}

// Put view controllers to tab bar
[tabBarController setViewControllers:[NSArray arrayWithObjects:
                                      [[FirstViewController alloc] init],
                                      [[SecondViewController alloc] init],
                                      [[ThirdViewController alloc] init],
                                      [[FourthViewController alloc] init],
                                      nil]];

[window addSubview:tabBarController.view];
[window makeKeyAndVisible];

How do I get TTNavigator to open the last opened tab, and if there's none - fallback to FirstViewController?

回答1:

So, I just found out about TTNavigatorDemo ;-)

Update: and also wrote a tutorial, see http://three20.pypt.lt/url-based-navigation-and-state-persistence