当选择表格中的一行时,它会切换到空白屏幕
在我的应用程序(故事板)的一部分中,我有一个包含各种行的表格视图,当我选择一行时,应该显示一个 html 文件。相反,当我选择一行时,我会看到一个空白屏幕。
我以为在 tableview 控制器之后我需要另一个视图控制器,但无论我是否有一个,我都会遇到同样的问题。
在我的表控制器 .m 文件中,我有以下代码:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
articleIndex = indexPath.row;
WebView *selectedArticle = [[WebView alloc] initWithNibName:nil bundle:nil];
[self.navigationController pushViewController:selectedArticle animated:YES];
[selectedArticle release];
这允许我选择一行并且屏幕更改为空白,而不是加载相关的 html 文件。
我的资源文件夹中有一个 .h 和 .m 文件,如下所示:
webView.h
#import <UIKit/UIKit.h>
#import "globals.h"
@interface WebView : UIViewController{
NSArray *articleNames;
IBOutlet UIWebView *webView;
}
@property(nonatomic, retain)IBOutlet UIWebView *webView;
@end
webView.m
#import "WebView.h"
@implementation WebView
@synthesize webView;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (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.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
articleNames = [[NSArray arrayWithObjects:
@"chapter1",
@"chapter2",
@"chapter3",
@"chapter4",
@"chapter5",
@"chapter6",
nil] retain];
NSString *chapterName = [[NSString alloc] initWithString:[articleNames objectAtIndex:articleIndex]];
NSString *path = [[NSBundle mainBundle] pathForResource:chapterName ofType:@"html"];
NSURL *url = [NSURL fileURLWithPath:path];
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url];
[webView loadRequest:urlRequest];
[chapterName release];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return YES;
}
@end
我看不到我在这里做错了什么,有什么建议吗?我不确定如何显示 html 文件,也不知道需要在哪里进行更改才能允许它。如果我从项目中删除 HTML 文件,应用程序就会崩溃,因此它肯定会加载它们,但由于某种原因不显示...
in part of my app (storyboard) I have a table view with various rows, and when I select a row, an html file should be displayed. Instead when I select a row, I get a blank screen instead.
I thought I needed another view controller after the tableview controller, but I get the same issue whether I have one or not.
In my table controller .m file I have the following code:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
articleIndex = indexPath.row;
WebView *selectedArticle = [[WebView alloc] initWithNibName:nil bundle:nil];
[self.navigationController pushViewController:selectedArticle animated:YES];
[selectedArticle release];
That is allowing me to select a row and the screen changes to a blank one, rather than loading the relevant html file.
I have in my resources folder a .h and .m file as follows:
webView.h
#import <UIKit/UIKit.h>
#import "globals.h"
@interface WebView : UIViewController{
NSArray *articleNames;
IBOutlet UIWebView *webView;
}
@property(nonatomic, retain)IBOutlet UIWebView *webView;
@end
webView.m
#import "WebView.h"
@implementation WebView
@synthesize webView;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (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.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
articleNames = [[NSArray arrayWithObjects:
@"chapter1",
@"chapter2",
@"chapter3",
@"chapter4",
@"chapter5",
@"chapter6",
nil] retain];
NSString *chapterName = [[NSString alloc] initWithString:[articleNames objectAtIndex:articleIndex]];
NSString *path = [[NSBundle mainBundle] pathForResource:chapterName ofType:@"html"];
NSURL *url = [NSURL fileURLWithPath:path];
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url];
[webView loadRequest:urlRequest];
[chapterName release];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return YES;
}
@end
I can't see what I have done wrong here, any advice? I'm not sure how to get the html files to display or where I need to make changes to allow it. If I remove the HTML files from the project the app crashes so it's definitely loading them but for some reason not displaying...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,“articleIndex”在哪里,我看到您在 tableViewController 中设置了它,但它不是 webView.h 或 webView.m。我建议首先添加 NSLog
这将让您首先查看 url 是否正确。其次,看起来您已将 UIWebView 与界面生成器连接起来,因此请确保 webView 连接正确。您还可以使用以下方法测试 webView 购买:
并查看 apple.com 是否加载。如果是这样,那么您就知道问题出在您的 html 文件或其路径上。
Well first off where is "articleIndex" I see you set it in your tableViewController but it is not webView.h or webView.m. I would suggest first adding in a NSLog
This will let you see if the url is correct first. Second it looks like you connected your UIWebView with interface builder, so make sure that the webView is connect correctly. You can also test the webView buy using:
And see if apple.com loads. If it does then you know your issue is with your html files or the paths to them.