2011-08-07 4 views
0

탭 막대로 앱을 만들고 있는데 탭 중 하나가 rss 리더이고 앱이 작동하지만 선택한 기사 콘텐츠를 표시하는 방법을 알 수 없습니다. UIWebview에서. 모든 것은 지금 셀이 선택 될 때를 제외하고는 UIWebView가있는 DetailViewController.xib에 푸시됩니다. 하지만 코드를 자동으로 기사의 URL을 찾습니다 그래서 나는 단지 UIWebView에 그것을 표시하는 방법을 알아낼 수 없습니다 그래서 그것을 설정했습니다. 또한 상대적으로 오브젝티브 C 멍청한 놈테이블 뷰에서 웹 뷰가있는 새 뷰 컨트롤러 밀어 넣기

#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]]]; 

(자신의 웹보기 이름으로 mywebview 교체를)

이 할 것인가

?

+0

나는 그것을 만들었고 이러한 오류가 발생합니다 : ':'앞에 '.' 토큰 및 이전 오류로 인해 혼란스러워 짐 –

+0

@matthew s - 오류가 발생했을 때의 예를 제공해 주시겠습니까? –

+0

NSString * requestURL = [NSString stringWithContentsOfURL : [NSURL URLWithString : storyLink] 인코딩 : NSUTF8StringEncoding 오류 : nil]; \t \t [DetailViewController.rssview loadRequest : [NSURLRequest requestWithURL : [NSURL URLWithString : requestURL]]]; 적어도 ":"오류가 DetailViewController.rssview 행에서 발생합니다. 더 유용 할 경우 전체 프로젝트에 대한 링크를 추가 할 수 있습니까? –

관련 문제