我已经设置了我的代码,以便当用户点击UITableView
中的单元格时,它会转移到WebViewController
,并沿途传递该单元格“Item URL”属性。在WebViewController
类中,我初始化一个UIWebView,并让它加载相应的URL。出于某种原因,它显示空白,不进行任何加载。如何将WebViewController
设置为在网页切换后开始加载网页?
WebViewController.h:
#import <UIKit/UIKit.h>
#import "MatchCenterViewController.h"
@interface WebViewController : UIViewController <UIWebViewDelegate>
@property (strong, nonatomic) NSURL *itemURL;
@property (strong, nonatomic) IBOutlet UIWebView *myWebView;
@end
WebViewController.m:
#import "WebViewController.h"
@interface WebViewController ()
@end
@implementation WebViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
_myWebView=[[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
_myWebView.delegate=self;
[self.view addSubview:_myWebView];
self.myWebView.delegate = self; //set the delegate first before calling LoadRequest
NSURL *url = [NSURL URLWithString:_itemURL];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[self.myWebView setScalesPageToFit:YES];
[self.myWebView loadRequest:request];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
MatchCenterViewController.h:
#import <UIKit/UIKit.h>
#import <Parse/Parse.h>
#import "AsyncImageView.h"
#import "SearchViewController.h"
#import "WebViewController.h"
@interface MatchCenterViewController : UIViewController <UITableViewDataSource>
//irrelevant code hidden for conciseness
@property (strong, nonatomic) NSArray *matchCenterArray;
@property (strong, nonatomic) NSString *searchTerm;
@property (strong, nonatomic) NSURL *itemURL;
@end
MatchCenterViewController.m:
#import "MatchCenterViewController.h"
#import <UIKit/UIKit.h>
@interface MatchCenterViewController () <UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *matchCenter;
@end
@implementation MatchCenterViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.matchCenter = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewCellStyleSubtitle];
self.matchCenter.frame = CGRectMake(0,50,320,self.view.frame.size.height-100);
_matchCenter.dataSource = self;
_matchCenter.delegate = self;
[self.view addSubview:self.matchCenter];
_matchCenterArray = [[NSArray alloc] init];
}
- (void)viewDidAppear:(BOOL)animated
{
self.matchCenterArray = [[NSArray alloc] init];
[PFCloud callFunctionInBackground:@"MatchCenter"
withParameters:@{
@"test": @"Hi",
}
block:^(NSArray *result, NSError *error) {
if (!error) {
_matchCenterArray = result;
[_matchCenter reloadData];
NSLog(@"Result: '%@'", result);
}
}];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return _matchCenterArray.count;
}
//the part where i setup sections and the deleting of said sections
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 21.0f;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
//irrelevant code removed for conciseness
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 3;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Initialize cell
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
// if no cell could be dequeued create a new one
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
//irrelevant code removed for conciseness
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 65;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSURL *itemURL = _matchCenterArray[indexPath.section][@"Top 3"][indexPath.row][@"Item URL"];
[self performSegueWithIdentifier:@"WebViewSegue" sender:self];
}
- (void)deleteButtonPressed:(id)sender
{
// links button
UIButton *deleteButton = (UIButton *)sender;
// Define the sections title
NSString *sectionName = _searchTerm = [[[[_matchCenterArray objectAtIndex:deleteButton.tag] objectForKey:@"Top 3"] objectAtIndex:3]objectForKey:@"Search Term"];
// Run delete function with respective section header as parameter
[PFCloud callFunctionInBackground:@"deleteFromMatchCenter"
withParameters:
@{@"searchTerm": sectionName,}
block:^(NSDictionary *result, NSError *error) {
if (!error) {
[PFCloud callFunctionInBackground:@"MatchCenter"
withParameters:@{
@"test": @"Hi",
}
block:^(NSArray *result, NSError *error) {
if (!error) {
_matchCenterArray = result;
[_matchCenter reloadData];
NSLog(@"Result: '%@'", result);
}
}];
}
}];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
WebViewController *controller = (WebViewController *) segue.destinationViewController;
controller.itemURL = self.itemURL;
}
@end
答案 0 :(得分:1)
您的代码中存在错误:
NSURL *url = [NSURL URLWithString:_itemURL]; NSURLRequest *request = [NSURLRequest requestWithURL:url];
_itemURL
是NSURL
个对象,而不是NSString
。你应该能够做到
NSURLRequest *request = [NSURLRequest requestWithURL:_itemURL];
已设置_itemURL
。
答案 1 :(得分:1)
您的代码中的这一行表示您的Xib文件中有一个WebView,并且您附加了一个插座:
@property (strong, nonatomic) IBOutlet UIWebView *myWebView;
那么当您已经在Xib文件中定义了webView时,为什么要创建另一个UIWebView并将其分配给IBOutlet的webView?分配新的webView并将其添加为子视图是没有意义的。也就是说,你应该丢弃这些行:
_myWebView=[[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
_myWebView.delegate=self;
[self.view addSubview:_myWebView];
原因是当你已经在Xib文件中有一个WebView时,你不需要它的另一个实例。只需:
self.myWebView.delegate = self; NSURL *url = [NSURL URLWithString: itemURL]; //itemURL must be a NSString. If it is a NSURL, then you should skip this line and put itemURL in the next line instead of "url" NSURLRequest *request = [NSURLRequest requestWithURL:url]; [self.myWebView setScalesPageToFit:YES]; [self.myWebView loadRequest:request];
答案 2 :(得分:0)
在WebViewController的viewDidLoad方法中,添加以下代码:
-(void)viewDidLoad {
[super viewDidLoad];
UIWebView* webView = [UIWebView alloc] initWithFrame:self.view.frame];
[self.view addSubView:webView];
NSURLRequest* request = [NSURLRequest requestWithURL:self.itemURL; cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:30];
[webView loadRequest:request];
}