1

This issue seems as if it is very hard to explain so I shall try my best.

I have several user profiles. I want them to be all handled by the same class.

TT://User/1
TT://User/2

How can I map it in a why were those both push to the user class.

In addition to that how can I tell user class what user ID to pull.

endy
  • 3,872
  • 5
  • 29
  • 43

1 Answers1

3

First, you need to map URLs to controllers. You typically do this in the AppDelegate since you want to have the URL map set up before calling a URL to display a view.

  • Instantiate a TTNavigator
  • Map the URLs to the controllers via TTURLMap
  • Always start with a wildcard URL ie. * and map it to a default view controller like TTWebController (the web browser view controller)
  • Basically, there are 2 types of URL: 1) URL without parameters and 2) URL w/ parameters. For the former, when URL is invoked, the initWithNibName:bundle: "constructor" of the mapped view controller will be called. For the latter, you can indicate what "init" method to call in the TTURLMap. See example below.
  • Actually opens an URL via openURLAction: method.

Here's the code.

@implementation AppDelegate

- (void)applicationDidFinishLaunching:(UIApplication *)application {
  TTNavigator* navigator = [TTNavigator navigator];

  TTURLMap* map = navigator.URLMap;

  // This is the default map. * = wildcard, any URL not registered will be
  // routed to the TTWebController object at runtime.  
  [map from:@"*" toViewController:[TTWebController class]];
  [map from:@"tt://catalog" toViewController:[CatalogController class]];
  [map from:@"tt://user/(initWithId:)" 
toViewController:[MyUserViewController class]];
  [map from:@"tt://user/(initWithId:)" 
toViewController:[MyUserViewController class]];

  if (![navigator restoreViewControllers]) {   
    [navigator openURLAction:[TTURLAction actionWithURLPath:@"tt://catalog"]];    
  }
}

// ...

@end

Second, subclass a TTViewController

@implementation MyUserViewController

- (id) initWithId:(NSNumber *)index {
  if (self = [super initWithNibName:nibName bundle:nil]) {
    // Do your initialization here.

    // Get the index from a singleton data manager containing the "model."
  }

  return self;
}

@end

Third, navigate to the URL from anywhere in the app.

// Navigate to the URL.
[[TTNavigator navigator] openURLAction:[TTURLAction actionWithURLPath:@"tt://user/1"]];
Cybersam
  • 236
  • 3
  • 3