2010-04-28 2 views
0

J'ai un projet iphone existant avec un UITabBar. Maintenant, j'ai besoin de texte stylisé et de liens vers d'autres ViewControllers dans mon application. J'essaie d'intégrer TTStyledTextLabel.tableView: didSelectRowAtIndexPath: appels TTNavigator openURLAction: applyAnimated: - UITabBar et navigationItem disparaissent

J'ai un FirstViewController: UITabelViewController avec ce tableView: didSelectRowAtIndexPath:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    NSString *url; 
    if ([self.filteredQuestions count]>0) { 
     url = [NSString stringWithFormat:@"%@%d", @"tt://question/", [[self.filteredQuestions objectAtIndex:indexPath.row] id]]; 

     [[TTNavigator navigator] openURLAction:[[TTURLAction actionWithURLPath: url] applyAnimated:YES]]; 
    } else { 
     Question * q = [self.questions objectAtIndex:indexPath.row] ; 
     url = [NSString stringWithFormat:@"%@%d", @"tt://question/", [q.id intValue]]; 
    } 

    TTDPRINT(@"%@", url); 
    TTNavigator *navigator = [TTNavigator navigator]; 
    [navigator openURLAction:[[TTURLAction actionWithURLPath: url] applyAnimated:YES]]; 

} 

Ma cartographie ressemble à ceci:

TTNavigator* navigator = [TTNavigator navigator]; 
navigator.window = window; 
navigator.supportsShakeToReload = YES; 
TTURLMap* map = navigator.URLMap; 
[map from:@"*" toViewController:[TTWebController class]]; 
[map from:@"tt://question/(initWithQID:)" toViewController:[QuestionDetailViewController class]]; 

et mon QuestionDetailViewController:

@interface QuestionDetailViewController : UIViewController <UIScrollViewDelegate , QuestionDetailViewProtocol> { 
    Question *question; 

} 

@property(nonatomic,retain) Question *question; 

-(id) initWithQID:(NSString *)qid; 
-(void) goBack:(id)sender; 

@end 

Quand je frappe une cellule, q QuestionDetailViewController sera appelé - mais le n avigationBar coutume

@implementation QuestionDetailViewController 
@synthesize question; 


-(id) initWithQID:(NSString *)qid 
{ 
    if (self = [super initWithNibName:@"QuestionDetailViewController" bundle:nil]) { 
    //; 
    TTDPRINT(@"%@", qid); 

    NSManagedObjectContext *managedObjectContext = [(domainAppDelegate*)[[UIApplication sharedApplication] delegate] managedObjectContext]; 

    NSPredicate *predicate =[NSPredicate predicateWithFormat:@"id == %@", qid]; 
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Question" 
               inManagedObjectContext:managedObjectContext]; 

    NSFetchRequest *request = [[NSFetchRequest alloc] init]; 
    [request setEntity:entity]; 
    [request setPredicate:predicate]; 
    NSError *error = nil; 
    NSArray *array = [managedObjectContext executeFetchRequest:request error:&error]; 

    if (error==nil && [array count]>0) { 
     self.question = [array objectAtIndex:0]; 
     } else { 
      TTDPRINT(@"error: %@", array); 
     } 
    } 
    return self; 
} 


- (void)viewDidLoad { 
    [super viewDidLoad]; 
    [TTStyleSheet setGlobalStyleSheet:[[[TextTestStyleSheet alloc] init] autorelease]]; 

    [self.navigationController.navigationBar setTranslucent:YES]; 
    NSArray *includedLinks = [self.question.answer.text vs_extractExternalLinks]; 

    NSMutableDictionary *linksToTT = [[NSMutableDictionary alloc] init]; 
    for (NSArray *a in includedLinks) { 
     NSString *s = [a objectAtIndex:3]; 
     if ([s hasPrefix:@"/answer/"] || [s hasPrefix:@"http://domain.com/answer/"] || [s hasPrefix:@"http://www.domain.com/answer/"]) { 
      NSString *ttAdress = @"tt://question/"; 
      NSArray *adressComps = [s pathComponents]; 
      for (NSString *s in adressComps) { 
       if ([s isEqualToString:@"qid"]) { 
        ttAdress = [ttAdress stringByAppendingString:[adressComps objectAtIndex:[adressComps indexOfObject:s]+1]]; 
       } 
      } 
      [linksToTT setObject:ttAdress forKey:s]; 
     } 
    } 
    for (NSString *k in [linksToTT allKeys]) { 
     self.question.answer.text = [self.question.answer.text stringByReplacingOccurrencesOfString:k withString: [linksToTT objectForKey:k]]; 
    } 

    TTStyledTextLabel *answerText = [[[TTStyledTextLabel alloc] initWithFrame:CGRectMake(0, 0, 320, 700)] autorelease]; 
    if (![[self.question.answer.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] hasPrefix:@"<div"]) { 
     self.question.answer.text = [NSString stringWithFormat:@"%<div>%@</div>", self.question.answer.text]; 
    } 

    NSString * s = [NSString stringWithFormat:@"<div class=\"header\">%@</div>\n%@",self.question.title ,self.question.answer.text]; 

    answerText.text = [TTStyledText textFromXHTML:s lineBreaks:YES URLs:YES]; 
    answerText.contentInset = UIEdgeInsetsMake(20, 15, 20, 15); 
    [answerText sizeToFit]; 

    [self.navigationController setNavigationBarHidden:NO animated:YES]; 
    [self.view addSubview:answerText]; 
    [(UIScrollView *)self.view setContentSize:answerText.frame.size]; 
    self.view.backgroundColor = [UIColor whiteColor]; 
    [linksToTT release]; 
} 


....... 

@end 

Cela fonctionne très bien, dès qu'une cellule est touchée, un QuestionDetailViewController est appelé et poussé - mais le tabBar disparaîtra, et la navigationItem - Je l'ai mis comme ceci: self.navigationItem.title [email protected]"back to first screen"; - ne sera pas être montré. Et il apparaît juste sans animation.

Mais si j'appuie sur un lien à l'intérieur du TTStyledTextLabel, l'animation fonctionne, l'élément navigationItem sera affiché.

Comment puis-je afficher l'animation, l'élément navigationItem et le tabBar?

Répondre

0

J'ai trouvé une solution:

Mon QuestionDetailViewController implémente le TTNavigatorDelegate.

-(BOOL)navigator:(TTNavigator *)navigator shouldOpenURL:(NSURL *)URL vaudra toujours NO, mais appellera [self.navigationController pushViewController:c animated:YES];

-(BOOL)navigator:(TTNavigator *)navigator shouldOpenURL:(NSURL *)URL { 
    NSEntityDescription *entity; 
    NSPredicate *predicate; 
    NSFetchRequest *request = [[NSFetchRequest alloc] init]; 
    if ([[URL host] isEqualToString:@"question"]) { 
     entity =[NSEntityDescription entityForName:@"Question" inManagedObjectContext:managedObjectContext]; 
     predicate = [NSPredicate predicateWithFormat:@"id == %@", [[URL path] stringByReplacingOccurrencesOfString:@"/" withString:@""]]; 

     [request setEntity:entity]; 
     [request setPredicate:predicate]; 

     NSError *error =nil; 
     NSArray *array = [managedObjectContext executeFetchRequest:request error:&error]; 
     if (error==nil && [array count] >0) { 
      QuestionDetailViewController *c = [[[QuestionDetailViewController alloc] init] autorelease]; 
      c.question = [array objectAtIndex:0]; 
      [self.navigationController pushViewController:c animated:YES]; 
     } 
    } 
    [request release]; 
    return NO; 
} 
0

Dans votre TableViewController, ajoutez:

- (id<UITableViewDelegate>)createDelegate { 
    return self; 
} 

Ensuite, vous pouvez mettre en œuvre vos propres méthodes de didSelectRowAtIndexPath et accessoryButtonTappedForRowWithIndexPath.

Questions connexes