2011-08-07 58 views
0

我正在製作一個帶有標籤欄的應用程序,其中一個標籤是rss閱讀器,應用程序可以工作,但我無法弄清楚如何顯示選定的文章內容在UIWebview中。現在一切工作正常,除了選擇一個單元格後,它將推送到其中包含UIWebView的DetailViewController.xib。但是我已經設置了這樣的代碼自動找到文章的URL,我只是不知道如何顯示它在UIWebView中。也比較Objective-C的小白用表格單元格的web視圖推新的視圖控制器

#import "RSSTableViewController.h" 
#import "OCVMobileAppDelegate.h" 
#import "DetailViewController.h" 

@implementation RSSTableViewController 
@synthesize detailView; 



#pragma mark - 
#pragma mark Initialization 

/* 
- (id)initWithStyle:(UITableViewStyle)style { 
    // Override initWithStyle: if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad. 
    self = [super initWithStyle:style]; 
    if (self) { 
     // Custom initialization. 
    } 
    return self; 
} 
*/ 


#pragma mark - 
#pragma mark View lifecycle 

/* 
- (void)viewDidLoad { 
    [super viewDidLoad]; 

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller. 
    // self.navigationItem.rightBarButtonItem = self.editButtonItem; 
} 
*/ 


- (void)viewWillAppear:(BOOL)animated { 
    [super viewWillAppear:animated]; 
    if ([stories count] == 0) { 
     NSString * path = @"http://feeds.feedburner.com/TheAppleBlog"; 
     [self parseXMLFileAtURL:path]; 
    } 
    cellSize = CGSizeMake([newsTable bounds].size.width, 60); 
} 

- (void)parseXMLFileAtURL:(NSString *)URL { 
    stories = [[NSMutableArray alloc] init]; 
    //you must then convert the path to a proper NSURL or it won't work 
    NSURL *xmlURL = [NSURL URLWithString:URL]; 
    // here, for some reason you have to use NSClassFromString when trying to alloc NSXMLParser, otherwise you will get an object not found error 
    // this may be necessary only for the toolchain 
    rssParser = [[NSXMLParser alloc] initWithContentsOfURL:xmlURL]; 
    // Set self as the delegate of the parser so that it will receive the parser delegate methods callbacks. 
    [rssParser setDelegate:self]; 
    // Depending on the XML document you're parsing, you may want to enable these features of NSXMLParser. 
    [rssParser setShouldProcessNamespaces:NO]; 
    [rssParser setShouldReportNamespacePrefixes:NO]; 
    [rssParser setShouldResolveExternalEntities:NO]; 
    [rssParser parse]; 
} 

- (void)parserDidStartDocument:(NSXMLParser *)parser { 
    NSLog(@"found file and started parsing"); 
} 
- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError { 
    NSString * errorString = [NSString stringWithFormat:@"Unable to download story feed from web site (Error code %i)", [parseError code]]; 
    NSLog(@"error parsing XML: %@", errorString); 
    UIAlertView * errorAlert = [[UIAlertView alloc] initWithTitle:@"Error loading content" message:errorString delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
    [errorAlert show]; 
} 
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{ 
    //NSLog(@"found this element: %@", elementName); 
    currentElement = [elementName copy]; 
    if ([elementName isEqualToString:@"item"]) { 
     // clear out our story item caches... 
     item = [[NSMutableDictionary alloc] init]; 
     currentTitle = [[NSMutableString alloc] init]; 
     currentDate = [[NSMutableString alloc] init]; 
     currentSummary = [[NSMutableString alloc] init]; 
     currentLink = [[NSMutableString alloc] init]; 
    } 
} 
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{ 
    //NSLog(@"ended element: %@", elementName); 
    if ([elementName isEqualToString:@"item"]) { 
     // save values to an item, then store that item into the array... 
     [item setObject:currentTitle forKey:@"title"]; 
     [item setObject:currentLink forKey:@"link"]; 
     [item setObject:currentSummary forKey:@"summary"]; 
     [item setObject:currentDate forKey:@"date"]; 
     [stories addObject:[item copy]]; 
     NSLog(@"adding story: %@", currentTitle); 
    } 
} 
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{ 
    //NSLog(@"found characters: %@", string); 
    // save the characters for the current item... 
    if ([currentElement isEqualToString:@"title"]) { 
     [currentTitle appendString:string]; 
    } else if ([currentElement isEqualToString:@"link"]) { 
     [currentLink appendString:string]; 
    } else if ([currentElement isEqualToString:@"description"]) { 
     [currentSummary appendString:string]; 
    } else if ([currentElement isEqualToString:@"pubDate"]) { 
     [currentDate appendString:string]; 
    } 
} 
- (void)parserDidEndDocument:(NSXMLParser *)parser { 
    [activityIndicator stopAnimating]; 
    [activityIndicator removeFromSuperview]; 
    NSLog(@"all done!"); 
    NSLog(@"stories array has %d items", [stories count]); 
    [newsTable reloadData]; 
} 
/* 
- (void)viewDidAppear:(BOOL)animated { 
    [super viewDidAppear:animated]; 
} 
*/ 
/* 
- (void)viewWillDisappear:(BOOL)animated { 
    [super viewWillDisappear:animated]; 
} 
*/ 
/* 
- (void)viewDidDisappear:(BOOL)animated { 
    [super viewDidDisappear:animated]; 
} 
*/ 
/* 
// Override to allow orientations other than the default portrait orientation. 
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
    // Return YES for supported orientations. 
    return (interfaceOrientation == UIInterfaceOrientationPortrait); 
} 
*/ 


#pragma mark - 
#pragma mark Table view data source 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    // Return the number of sections. 
    return 1; 
} 


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    // Return the number of rows in the section. 
    return [stories count]; 
} 


// Customize the appearance of table view cells. 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *MyIdentifier = @"MyIdentifier"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease]; 
    } 

    // Configure the cell... 
    int storyindex = [indexPath indexAtPosition:[indexPath length]-1]; 
    [cell setText:[[stories objectAtIndex:storyindex]objectForKey:@"title"]]; 
    return cell; 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    int storyIndex = [indexPath indexAtPosition: [indexPath length] - 1]; 
    NSString * storyLink = [[stories objectAtIndex: storyIndex] objectForKey: @"link"]; 
    // clean up the link - get rid of spaces, returns, and tabs... 
    storyLink = [storyLink stringByReplacingOccurrencesOfString:@" " withString:@""]; 
    storyLink = [storyLink stringByReplacingOccurrencesOfString:@"\n" withString:@""]; 
    storyLink = [storyLink stringByReplacingOccurrencesOfString:@" " withString:@""]; 
    NSLog(@"link: %@", storyLink); 
    if(indexPath.row==0) 
    { 
     DetailViewController *newviewController=[[[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil]autorelease]; 
     [self.navigationController pushViewController:newviewController animated:YES]; 
    } 
    else { 
     DetailViewController *newviewController=[[[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil]autorelease]; 
     [self.navigationController pushViewController:newviewController animated:YES]; 
    } 

} 



/* 
// Override to support conditional editing of the table view. 
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { 
    // Return NO if you do not want the specified item to be editable. 
    return YES; 
} 
*/ 


/* 
// Override to support editing the table view. 
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { 

    if (editingStyle == UITableViewCellEditingStyleDelete) { 
     // Delete the row from the data source. 
     [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
    } 
    else if (editingStyle == UITableViewCellEditingStyleInsert) { 
     // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view. 
    } 
} 
*/ 


/* 
// Override to support rearranging the table view. 
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath { 
} 
*/ 


/* 
// Override to support conditional rearranging of the table view. 
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath { 
    // Return NO if you do not want the item to be re-orderable. 
    return YES; 
} 
*/ 


#pragma mark - 
#pragma mark Table view delegate 




#pragma mark - 
#pragma mark Memory management 

- (void)didReceiveMemoryWarning { 
    // Releases the view if it doesn't have a superview. 
    [super didReceiveMemoryWarning]; 

    // Relinquish ownership any cached data, images, etc. that aren't in use. 
} 

- (void)viewDidUnload { 
    // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand. 
    // For example: self.myOutlet = nil; 
} 


- (void)dealloc { 
    [currentElement release]; 
    [rssParser release]; 
    [stories release]; 
    [item release]; 
    [currentTitle release]; 
    [currentDate release]; 
    [currentSummary release]; 
    [currentLink release]; 
    [super dealloc]; 
} 


@end 

回答

0
NSString *requestURL = [NSString stringWithContentsOfURL:[NSURL URLWithString:storyLink] encoding:NSUTF8StringEncoding error:nil]; 
[newviewcontroller.mywebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:requestURL]]]; 

(用自己的WebView名稱替換mywebview

請問這怎麼辦?

+0

我做到了這一點,我得到這些錯誤: 預期':'之前'。'令牌和救助困惑的早期錯誤 –

+0

@matthew s - 你可以提供一個錯誤發生的例子嗎? –

+0

NSString * requestURL = [NSString stringWithContentsOfURL:[NSURL URLWithString:storyLink] encoding:NSUTF8StringEncoding error:nil]; \t \t [DetailViewController.rssview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:requestURL]]]; 它發生了,至少在DetailViewController.rssview行發生':'錯誤,我可以添加一個鏈接到整個項目,如果這會更有用? –

相關問題