我使用的是IFTweetLabel,它能识别链接,但我很难用IFTweetLabel创建的按钮打开一个网页视图。我正在运行NSLog,可以清楚地看到它正在理解每个链接,当按钮被按下时,但由于某种原因,它不会打开网址。
下面是我用来显示视图和在webView....which中加载字符串的代码,除了加载webview之外,所有这些都是有效的。
如有任何建议,我们将不胜感激!谢谢!
- (void)handleTweetNotification:(NSNotification *)notification
{
[UIView beginAnimations:@"animateView" context:nil];
[UIView setAnimationDuration:1.0];
CGRect viewFrame = [MainwebView frame];
viewFrame.origin.x = 220;
MainwebView.frame = viewFrame;
MainwebView.alpha = 1.0;
web.alpha = 1.0;
MainwebView.layer.shadowColor = [[UIColor blackColor] CGColor];
MainwebView.layer.shadowOffset = CGSizeMake(1.0f, 1.0f);
MainwebView.layer.shadowRadius = 8.0f;
MainwebView.layer.shadowOpacity = 1.0f;
[self.view addSubview:MainwebView];
[UIView commitAnimations];
[web loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:tweetLabel.text]]];
NSLog(@"handleTweetNotification: WTF notification = %@", notification);
}发布于 2012-02-14 05:24:56
以下是我的代码,它们应该工作得很好,但也应该进行一些清理:
- (void)handleTweetNotification:(NSNotification *)notification {
unichar theChar = [(NSString *)notification.object characterAtIndex:0];
NSString *theString = (NSString *)notification.object;
if ( [[NSString stringWithCharacters:&theChar length:1] isEqualToString:@"#"]) {
DLog(@"This is a hashtag");
theString = [theString stringByReplacingOccurrencesOfString:@"#" withString:@"%23"];
NSURL *hashtagURL = [NSURL URLWithString:[NSString stringWithFormat:@"https://twitter.com/#!/search?q=%@", theString]];
WebViewController *theWebVC = [[WebViewController alloc] init];
theWebVC.request = [NSURLRequest requestWithURL:hashtagURL];
UINavigationController *theNavigationVC = [[UINavigationController alloc] initWithRootViewController:theWebVC];
[self presentModalViewController:theNavigationVC animated:YES];
}
if ( [[NSString stringWithCharacters:&theChar length:1] isEqualToString:@"@"]) {
DLog(@"This is a Mention");
theString = [theString stringByReplacingOccurrencesOfString:@"@" withString:@""];
NSURL *mentionURL = [NSURL URLWithString:[NSString stringWithFormat:@"https://twitter.com/%@", theString]];
WebViewController *theWebVC = [[WebViewController alloc] init];
theWebVC.request = [NSURLRequest requestWithURL:mentionURL];
UINavigationController *theNavigationVC = [[UINavigationController alloc] initWithRootViewController:theWebVC];
[self presentModalViewController:theNavigationVC animated:YES];
}
if ( [[NSString stringWithCharacters:&theChar length:1] isEqualToString:@"h"]) {
DLog(@"This is a hyperlink");
theString = [[theString componentsSeparatedByString: @"\n"] objectAtIndex:0];
NSURL *hyperlinkURL = [NSURL URLWithString:[NSString stringWithFormat:@"%@", theString]];
WebViewController *theWebVC = [[WebViewController alloc] init];
theWebVC.request = [NSURLRequest requestWithURL:hyperlinkURL];
UINavigationController *theNavigationVC = [[UINavigationController alloc] initWithRootViewController:theWebVC];
[self presentModalViewController:theNavigationVC animated:YES];
}
}发布于 2011-08-20 16:20:34
用户单击的字符串将传递给此方法,并可通过使用通知对象找到。您在代码中所做的就是将tweetLabel中的所有文本传递给webview。由于这不是一个URL,你的应用程序将崩溃。改用这个:[web loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[notification object]]]];
您也可以首先对通知对象执行NSLog操作,以确保它是一个URL。
https://stackoverflow.com/questions/6620151
复制相似问题