tags:

views:

1709

answers:

2

All I want is to display some simple text in my viewController and have the hyperlinks autoparsed. When the user clicks on the link I want the control to somehow do a callback where I can do something with the URL. How can I achieve this?

I've already looked through the TTCatalog for hours. I have also tried looking into the source code of three20 as well as looking at the stack trace. No help. I just can't figure out how my app can react to the click of the URL. Any hints please?

+6  A: 

Hard to help without seeing what you've already tried, but you should be able to do something like the following:

TTStyledTextLabel* label = [[[TTStyledTextLabel alloc] 
        initWithFrame:someFrame] autorelease];
NSString* labelText = @"This should <a href=\"custom-uri://some/url\">work</a>";
label.text = [TTStyledText textFromXHTML:labelText lineBreaks:NO URLs:YES];
[someView addSubview:label];

You can then use TTNavigator and TTURLMap to map custom-uri://some/url to a particular controller in your application, or handle it yourself in your application delegate. The best place to find out how to do that is by looking at the TTNavigatorDemo sample application included in the Three20 source. Specifically, look at AppDelegate.m which is where all the URL mapping gets performed.

Nathan de Vries
Great start! Can you give a brief example of how to use TTNavigator and TTURLMap to say push a controller when the URL is clicked?
erotsppa
I've updated my answer to point you in the right direction for finding out how the URL mapping stuff works.
Nathan de Vries
This url mapping system wouldn't work for this question, correct? It seems like the url have to be preset by the developer, and does not work with text that are given by the user. I want the TTStyledTextLabel to display some text from the user, and have all the hyperlinks parsed and perform a single same action when clicked
erotsppa
Your question has zero details about how the text is actually entered or what the URLs are, so it's impossible for me to know whether or not you can do what you want. You can map any URL, so it's likely that you can do what you're after, but you're going to need to read the sample code like I told you in my answer.
Nathan de Vries
The one other element that might be missing from this answers is that, when the link in the label invokes the `TTNavigator`, it will also ask its delegate whether the URL should be opened. So that's a way to listen for arbitrary links, without specifically mapping their destinations (as is done with the in-app navigation demonstrated in the TTNavigatorDemo).
Sixten Otto
Hey, any answers to my query?
Raj
A: 

I have followed Nathan's steps and I am able to get the click event and load a view controller using this:

TTNavigator* navigator = [TTNavigator navigator];
TTURLMap* map = navigator.URLMap;
[map from:@"*" toViewController:[URLHandlerViewController class]];

But I have a problem - how to get the URL inside URLHandlerViewController once it is loaded?

Raj
Raj, you need to map different URLs to different view controllers. Don't worry about handling the URL, TTNavigator is meant to do that for you.
Paul Shapiro
Raj