2010-07-08 413 views
4

確定,而不是試圖解釋我的問題在文本中,看這個小視頻我記錄 http://www.youtube.com/watch?v=fwb55jnZI6w打開鏈接到Safari瀏覽器(iPhone)

這裏是詳細信息視圖控制器代碼(網頁,其中網頁視圖是)

detailViewController.h

#import <UIKit/UIKit.h> 
#import "MWFeedItem.h" 
@interface DetailViewController : UIViewController <UIWebViewDelegate> { 
    MWFeedItem *item; 
    NSString *summaryString; 
    IBOutlet UILabel *titleLabel; 
    IBOutlet UIWebView *contentLabel; 
    //IBOutlet UILabel *dateLabel; 
    IBOutlet UIScrollView *textScroller; 
} 
@property (nonatomic, retain) MWFeedItem *item; 
@property (nonatomic, retain) NSString *summaryString; 
@property (nonatomic, retain) IBOutlet UILabel *titleLabel; 
@property (nonatomic, retain) IBOutlet UIWebView *contentLabel; 
@end 

detailViewController.m

#import "DetailViewController.h" 
#import "NSString+XMLEntities.h" 

typedef enum { SectionHeader, SectionDetail } Sections; 
typedef enum { SectionHeaderTitle, SectionHeaderDate, SectionHeaderURL } HeaderRows; 
typedef enum { SectionDetailSummary } DetailRows; 



@implementation DetailViewController 
@synthesize item, summaryString, titleLabel, contentLabel; 


- (BOOL)webView:(UIWebView *)contentLabel shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType; { 

    NSURL *requestURL = [ [ request URL ] retain ]; 
    // Check to see what protocol/scheme the requested URL is. 

     return [ [ UIApplication sharedApplication ] openURL: [ requestURL autorelease ] ]; 
    return NO; 
    // Auto release 
    [ requestURL release ]; 
    // If request url is something other than http or https it will open 
    // in UIWebView. You could also check for the other following 
    // protocols: tel, mailto and sms 
} 
- (void)viewDidLoad { 

    [super viewDidLoad]; 

    /*if (item.date) { 
     NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; 
     [formatter setDateStyle:NSDateFormatterMediumStyle]; 
     [formatter setTimeStyle:NSDateFormatterMediumStyle]; 
     self.dateString = [formatter stringFromDate:item.date]; 
     [formatter release]; 
    }*/ 

    if (item.summary) { 
     self.summaryString = [[[item.summary stringByStrippingTags] stringByRemovingNewLinesAndWhitespace] stringByDecodingXMLEntities]; 
    } 

     titleLabel.text = item.title ? item.title : @"[No Title]"; 
     [titleLabel setBackgroundColor:[UIColor clearColor]]; 
     //dateLabel.text = dateString ? dateString : @"[No Date]"; 
          // Summary 
    NSString *HTMLData = summaryString; 
    [contentLabel loadHTMLString:HTMLData baseURL:[NSURL URLWithString: [NSString stringWithFormat:@"http://www.feed43.com/1515171705611023.xml"]]]; 

    //Calculate the expected size based on the font and linebreak mode of your label 
    //CGSize maximumLabelSize = CGSizeMake(280,9999); 

    //CGSize expectedLabelSize = [summaryString sizeWithFont:contentLabel.font 
    //constrainedToSize:maximumLabelSize 
    //lineBreakMode:contentLabel.lineBreakMode]; 


    //adjust the label the the new height. 
    //CGRect newFrame = contentLabel.frame; 
    //newFrame.size.height = expectedLabelSize.height; 
    //contentLabel.frame = newFrame; 
    [textScroller setCanCancelContentTouches:NO]; 
    [textScroller setContentSize:CGSizeMake(320, 500)]; 
    textScroller.indicatorStyle = UIScrollViewIndicatorStyleBlack; 
    textScroller.scrollEnabled = YES; 
    textScroller.clipsToBounds = YES; 

    //return titleLabel; 
    //return dateLabel; 
    //return contentLabel; 

} 
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
    // Return YES for supported orientations 
    //return (interfaceOrientation == UIInterfaceOrientationPortrait); 
    return NO; 
} 
- (void)dealloc { 
    [super dealloc]; 
} 
@end 

注意:即使它說「contentLabel」它是一個UIWebView。它是從舊版本遺留下來的

謝謝!

回答

17

實施-(BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType
然後,你可以做這樣的事情:

NSURL* url = [request URL]; 
if (UIWebViewNavigationTypeLinkClicked == navigationType) 
{ 
    [[UIApplication sharedApplication] openURL:url]; 
    return NO; 
} 
+1

謝謝!我唯一需要改變的地方是該代理實現中的第二個「webView」到「contentLabel」(我的webView的名稱),然後把「return YES;」在你展示給我的代碼的花括號之外。謝謝! – 2010-07-09 04:14:13

+0

+1;這幫助了我今天。 – 2011-04-11 17:02:44

相關問題