understanding TTNavigator
Asked Answered
B

4

9

following situation:

in a TTTableViewController i added some Cells with URLs. they are opening a class with @"tt://photos" for example. this works quite fine.

the first thing is, i saw some urls in TT Examples like @"tt/photos/1". is it possible to fetch this "1" in my photos class and say, for example okay, please open picture one, ore is this only another URL that was declared in TTNavigatior to open a specific Class?

the other thing is: is it possible to forward an object to the linked class? clicking a cell opens @"tt://photos" (the linked class in my TTNavigator)

working with normal tableviews i can overwrite my init method and send an object with my initialize method, is this also possible by clicking my TTItems?

thanks!

Blackstone answered 23/2, 2010 at 11:42 Comment(0)
B
21

figured it out myself, for those who need it:

First (passing "subURLs" in your navigator map)

navigating to an URL with @"tt://photos/firstphoto" is possible, you can fetch the "firstphoto" like this:

//Prepare your Navigator Map like this
[map from:@"tt://photos/(initWithNumber:)" toViewController:[PhotoVC class]];

In your PhotoVC you can access this Number:

-(void) initWithNumber: (NSString*)number {
    NSLog(@"%@",number);
}

calling your View Controller with this url would look:

PhotoVC* controller = [[PhotoVC alloc] initWithNumber:@"1"];
[navigationController pushViewController:controller animated:YES];
[controller release];

Second (passing objects in an TTTableViewController)

its a bit tricky, but you dont have to Subclass anything.

first, nil the URL in the TableItem

 [TTTableLink itemWithText:@"TTTableLink" URL:nil]

in your TTTableViewController write down this method

- (void)didSelectObject:(id)object atIndexPath:(NSIndexPath*)indexPath {
     TTURLAction *urlAction = [[[TTURLAction alloc] initWithURLPath:@"tt://photos"] autorelease];
     urlAction.query = [NSDictionary dictionaryWithObject:@"firstphoto" forKey:@"photo"];
     urlAction.animated = YES;
     [[TTNavigator navigator] openURLAction:urlAction];
 }

now in your your PhotoVC you need something like this

- (id)initWithNavigatorURL:(NSURL*)URL query:(NSDictionary*)query {
     if (self = [super init]) {
              NSLog(@"%@",query);
}

     return self;
}

and you are done ;)

Blackstone answered 23/2, 2010 at 18:46 Comment(2)
Awesome! I wasn't passing nil in ` [TTTableLink itemWithText:@"TTTableLink" URL:nil]`. It's stupid that you have to do that, since now I have to manually tell the cell to display the right arrow.Erroneous
Choise, you can use URL as expected. See my answer on your question. I've changed your code to be TTNavigator conform.Maryrosemarys
P
3

I was trying to implement choise's answer, learned a lot, and eventually had to get the callouts showing up and keep the implementation with many urls simple, so here's what i did.

  1. Keep URL in the TableItem,

  2. Use this code in the TTTableViewController subclass.

    - (void)didSelectObject:(id)object atIndexPath:(NSIndexPath*)indexPath {
    
          NSLog(@"Its url is %@", [object URL]);
    
          TTURLAction *urlAction = [[[TTURLAction alloc] initWithURLPath:(NSString *)[object URL]] autorelease];
    
          urlAction.query = [NSDictionary dictionaryWithObject:self.user forKey:@"user"];
    
          urlAction.animated = YES;
    
          [[TTNavigator navigator] openURLAction:urlAction];
    
     }
    
     - (BOOL)shouldOpenURL:(NSString*)URL {
    
         return NO;
    
     }
    
  3. That "shouldOpenURL:" was discovered looking through TTTableViewController, I tried it out, and it worked. Now the table view is not opening a duplicate view, and there are callouts!

Thanks choise!

Penninite answered 31/12, 2010 at 9:25 Comment(1)
Yes, I also just discovered this the hard way. Overriding shouldOpenURL: to return NO is a much better solution than passing nil to the URL parameter of the TTLinkedItem. That way you get to keep the right arrows in the table view cells.Erroneous
J
0

Although choice's answer works for multiple params when u are creating the TTURLAction in code it is not very useful when u want to embed links to view controllers in your TTStyledLabel.One solution to that is to use multiple params in a single string.

<a href='app://view2/param1=value1&param2=value2&...'>LabelName</a>

if you want the code to parse such urls and get the params please feel free to send me a message and I will send you my parser classes. (or you can build your own with NSScanner!)

Also dont forget to escape the &s otherwise TTStyledLabel would not like it!

Jeanejeanelle answered 17/5, 2011 at 21:8 Comment(2)
yes, you can also use "get-like" params app://myviewcontroller?foo=bar&lorem=ipsumBlackstone
you are right...i just found out that app://view2/-param1-param2-param3- doesnt really work...TTNavigator converts it into http://app://view2/-param1-param2-param3- since it does not look like a URL.I will edit this now.Jeanejeanelle
M
0

You don't need to run this on current version 1.0.6.2 for TTTableViewController. The "URL" option is working as expected. If it's not working for you, then your URL is broken or your are calling the wrong function on your ViewController. The function you have to call through URL must return an id (be a constructor for a ViewController) of a ViewController. Then it'll work as expected.

I'll changed the example form choise to be like TTNavigator expect it to be.

Add a mapping, which TTNavigator will use to navigate:

//Prepare your Navigator Map like this
[map from:@"tt://photos/(initWithNumber:)" toViewController:[PhotoVC class]];

Create a TTTableLink (or TTStyledText, or other) with an URL set, which should mach your map:

[TTTableLink itemWithText:@"TTTableLink" URL:@"tt://photos/1"]

Add this to your PhotoVC to be called by TTNavigator on the given URL

-(id) initWithNumber: (NSString*)number {
    if (self = [super init]) {
        self.title = @"Some Title";

        NSLog(@"%@",number);
    }

    return self;       
}

You don't need to overwrite the function didSelectObject, as the TTNavigator will call your ViewController through defined constructor function tt://photos/(initWithNumber:)

Maryrosemarys answered 16/8, 2012 at 19:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.