understanding TTNavigator

一个人想着一个人 提交于 2020-01-22 04:33:32

问题


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!


回答1:


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 ;)




回答2:


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!




回答3:


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!




回答4:


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:)



来源:https://stackoverflow.com/questions/2317754/understanding-ttnavigator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!