我一直在尝试iOS应用程序一段时间,并没有真正花太多时间投入其中。我现在正在撞墙,因为我无法弄清楚如何使这个工作或我没有正确配置...我试图获取NewsTableViewController中引用的bodytext加载到NewsView上的textView控制器。目前只有标题更新和textview只显示lorum ipsum文本。
我的理解是因为我可以看到NSLog中的信息,如果我尝试将新闻正文放在推送视图的标题中,它会显示在标题中 - 我的想法是我无法定义视图但是正如我所说,我只是看不到它!无论如何,这就是我所拥有的......
这是将数据从xml文件加载到第一个视图的表
//
// NewsTableViewController.h
//
#import <UIKit/UIKit.h>
#import "MBProgressHUD.h"
@interface NewsTableViewController : UITableViewController
{
IBOutlet UITableView *newsTable;
CGSize cellSize;
NSXMLParser *rssParser;
NSMutableArray *stories;
NSMutableDictionary *item;
NSString *currentElement;
NSMutableString *currentName, *currentTitle, *currentDated, *currentBodyText;
}
- (UITableViewCell *) getCellContentView:(NSString *)MyIdentifier;
@end
实施代码
//
// NewsTableViewController.m
//
#import "NewsTableViewController.h"
#import "NewsViewController.h"
@interface NewsTableViewController ()
@end
@implementation NewsTableViewController
dispatch_queue_t myQueue;
-(void) showHUD{
MBProgressHUD *HUD;
HUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view];
[self.navigationController.view addSubview:HUD];
//HUD.delegate = self;
HUD.labelText = @"News Loading";
HUD.detailsLabelText = @"please wait...";
HUD.square = YES;
HUD.dimBackground = YES;
[HUD showWhileExecuting:@selector(parserStart) onTarget:self withObject:nil animated:YES];
//dispatch_async(dispatch_get_main_queue(), ^ {[self parserStart]; });
}
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
//NSLog(@"View Did Appear");
myQueue = dispatch_queue_create("com.xxxxxxxxxxx.xxxxxxxxxx",NULL);
dispatch_async(dispatch_get_main_queue(), ^ {[self showHUD]; });
}
- (void) parserStart {
//Insert a small delay for testing purposes
//[NSThread sleepForTimeInterval:2];
if ([stories count] == 0) {
NSString *path = @"http://xxx.xxxxxxxxxxx.xxx/xxxxxx/xxxxxxx.xml";
[self parseXMLFileAtURL:path];
//[path release];
}
cellSize = CGSizeMake([newsTable bounds].size.width, 60);
[self.tableView reloadData];
}
- (void)parseXMLFileAtURL:(NSString *)URL {
if (stories) {
//[stories release];
stories = nil;
}
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 the news 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);
//if (currentElement) {
//[currentElement release];
//currentElement = nil;
//}
currentElement = [elementName copy];
if ([elementName isEqualToString:@"article"]) {
// clear out our story item caches...
item = [[NSMutableDictionary alloc] init];
currentName = [[NSMutableString alloc] init];
currentTitle = [[NSMutableString alloc] init];
currentDated = [[NSMutableString alloc] init];
currentBodyText = [[NSMutableString alloc] init];
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
//NSLog(@"found characters: %@", string);
// save the characters for the current item...
if ([currentElement isEqualToString:@"article"]) {
[currentName appendString:string];
} else if ([currentElement isEqualToString:@"title"]) {
[currentTitle appendString:string];
} else if ([currentElement isEqualToString:@"dated"]) {
[currentDated appendString:string];
} else if ([currentElement isEqualToString:@"bodytext"]) {
[currentBodyText appendString:string];
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
//NSLog(@"ended element: %@", elementName);
if ([elementName isEqualToString:@"article"]) {
// save values to an item, then store that item into the array...
[item setObject:currentName forKey:@"article"];
[item setObject:currentTitle forKey:@"title"];
[item setObject:currentDated forKey:@"dated"];
[item setObject:currentBodyText forKey:@"bodytext"];
[stories addObject:[item copy]];
//NSLog(@"adding story: %@", currentName);
}
}
- (void)parserDidEndDocument:(NSXMLParser *)parser {
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
#pragma mark Table view methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)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 = [self getCellContentView:MyIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
UILabel *lblTitle = (UILabel *)[cell viewWithTag:101];
UILabel *lblDate = (UILabel *)[cell viewWithTag:102];
UILabel *lblBodyText = (UILabel *)[cell viewWithTag:103];
int storyIndex = [indexPath indexAtPosition: [indexPath length] - 1];
//NSString *articleValue = [[stories objectAtIndex: storyIndex] objectForKey: @"article"];
NSString *titleValue = [[stories objectAtIndex: storyIndex] objectForKey: @"title"];
NSString *datedValue = [[stories objectAtIndex: storyIndex] objectForKey: @"dated"];
NSString *bodytextValue = [[stories objectAtIndex: storyIndex] objectForKey: @"bodytext"];
lblTitle.text = titleValue;
lblDate.text = datedValue;
lblBodyText.text = bodytextValue;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"NewsSegue"]) {
// note that "sender" will be the tableView cell that was selected
UITableViewCell *cell = (UITableViewCell*)sender;
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
NewsViewController *nvc = [segue destinationViewController];
int storyIndex = [indexPath indexAtPosition: [indexPath length] - 1];
nvc.title = [[stories objectAtIndex: storyIndex] objectForKey: @"title"];
nvc.textView.text = [[stories objectAtIndex: storyIndex] objectForKey: @"bodytext"];
//nvc.textView.text = [self getDataToPass:storyIndex.row];
// hide the tabBar Controller
nvc.hidesBottomBarWhenPushed = YES;
//NSLog(@"Article : %@", [[stories objectAtIndex:storyIndex] objectForKey: @"article"]);
NSLog(@"Title : %@", [[stories objectAtIndex:storyIndex] objectForKey: @"title"]);
NSLog(@"Dated : %@", [[stories objectAtIndex:storyIndex] objectForKey: @"dated"]);
NSLog(@"BodyText : %@", [[stories objectAtIndex:storyIndex] objectForKey: @"bodytext"]); }
}
- (void)dealloc {
}
@end
现在我正在推动的观点......
//
// NewsViewController.h
//
#import <UIKit/UIKit.h>
@class NewsViewController;
@interface NewsViewController : UIViewController {
IBOutlet UITextView* textView;
}
@property (nonatomic, retain) IBOutlet UITextView* textView;
@end
然后是该视图的实现文件。
//
// NewsViewController.m
//
#import "NewsViewController.h"
#import "NewsTableViewController.h"
@interface NewsViewController ()
@end
@implementation NewsViewController
@synthesize textView;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.textView = self.title;
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
@end
编辑:据我所知,Segue的这一部分是我从附加到解析器的代码的发送部分发送信息的地方:
nvc.title = [[stories objectAtIndex: storyIndex] objectForKey: @"title"];
nvc.textView.text = [[stories objectAtIndex:storyIndex] objectForKey: @"bodytext"];
如果我将bodytext设置为标题信息显示,那么为什么我认为文本视图中有一些不正确的东西,这就是我被卡住了?
任何帮助都会受到赞赏,因为我真的不知道出了什么问题!我实际上希望我明白我错过了什么!谢谢你的期待。
答案 0 :(得分:1)
我在准备工作中添加了以下内容
[nvc setTextFieldContentText:[[stories objectAtIndex:storyIndex] objectForKey: @"bodytext"]];
然后在View中加载了接收视图
[textView setText:[self textFieldContentText]];
显然在接收视图头文件中设置属性
@property NSString* textFieldContentText;
感谢所有花时间寻求帮助的人。
答案 1 :(得分:0)
需要设置textView的文本属性。试试这个:
self.textView.text = self.title;
答案 2 :(得分:0)
我对这行代码尝试完成的内容感到有点困惑:self.textView = self.title;
但无论如何,我认为您的问题是您正在尝试为尚不存在的视图设置文本。尝试在新闻视图控制器中创建NSString
(例如textViewString
)并在prepareForSegue
中设置,然后在viewDidLoad
或viewWillAppear
做{{1} }}