我想使用在我的主视图中解析的数据,主视图是导航表视图控制器,我想使用在每个帖子的详细视图中解析的数据。我不知道如何做到这一点,我需要帮助。谢谢:))
的 MasterViewController.h 的
#import <UIKit/UIKit.h>
@interface MasterViewController : UITableViewController <NSXMLParserDelegate>
@property NSMutableArray *posts;
@property NSMutableString *currentStr;
@property NSMutableDictionary *currentDic;
@end
的 MasterViewController.m 的
#import "MasterViewController.h"
#import "DetailViewController.h"
@interface MasterViewController () {
NSMutableArray *_objects;
}
@end
@implementation MasterViewController
@synthesize currentDic, currentStr, posts;
- (void)awakeFromNib
{
[super awakeFromNib];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSURL *url = [[NSURL alloc] initWithString:@"http://devlabs.es/feeds/"];
posts = [[NSMutableArray alloc]init];
NSXMLParser *parserFeed = [[NSXMLParser alloc] initWithContentsOfURL:url];
parserFeed.delegate = self;
[parserFeed parse];
}
- (void) parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
if ([elementName isEqualToString:@"item"]) {
currentDic = [[NSMutableDictionary alloc]init];
currentStr = [[NSMutableString alloc]init];
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
if ([elementName isEqualToString:@"title"]) {
[currentDic setValue:currentStr forKey:@"titulo"];
} if ([elementName isEqualToString:@"link"]) {
[currentDic setValue:currentStr forKey:@"link"];
} if ([elementName isEqualToString:@"description"]) {
[currentDic setValue:currentStr forKey:@"description"];
} if ([elementName isEqualToString:@"item"]) {
[posts addObject:currentDic];
}
currentStr = nil;
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if (!currentStr) {
currentStr = [[NSMutableString alloc]initWithString:string];
} else {
[currentStr appendString:string];
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table View
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [posts count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
NSMutableDictionary *dic = [posts objectAtIndex:indexPath.row];
cell.textLabel.text = [dic objectForKey:@"titulo"];
cell.detailTextLabel.text = [dic objectForKey:@"description"];
return cell;
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
[_objects removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:@[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.
}
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:@"showDetail"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
NSDate *object = _objects[indexPath.row];
[[segue destinationViewController] setDetailItem:object];
}
}
@end
的 DetailViewController.h 的
#import <UIKit/UIKit.h>
#import "MasterViewController.h"
@interface DetailViewController : UIViewController
@property (strong, nonatomic) id detailItem;
@property (weak, nonatomic) IBOutlet UILabel *descriptionLabel;
@property (nonatomic, strong) IBOutlet UILabel *titleLabel;
@end
的 DetailViewController.m 的
#import "DetailViewController.h"
#import "MasterViewController.h"
@interface DetailViewController ()
- (void)configureView;
@end
@implementation DetailViewController
@synthesize descriptionLabel, titleLabel;
#pragma mark - Managing the detail item
- (void)setDetailItem:(id)newDetailItem
{
if (_detailItem != newDetailItem) {
_detailItem = newDetailItem;
// Update the view.
[self configureView];
}
}
- (void)configureView
{
// Update the user interface for the detail item.
if (self.detailItem) {
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self configureView];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end