ios - 网站未在 UIWebView 中加载

标签 ios objective-c uitableview uiwebview

我已经设置了我的代码,这样当用户点击 UITableView 中的单元格时,它会转至 WebViewController,并传递该单元格的“Item URL”属性一路上。在 WebViewController 类中,我初始化了一个 UIWebView,并让它加载相应的 URL。由于某种原因,它显示为空白并且不进行任何加载。如何将我的 WebViewController 设置为在 segues 结束后开始加载网页?

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

最佳答案

您代码中的这一行表明您的 Xib 文件中有一个 WebView,并且您有一个 outlet 附加到它:

@property (strong, nonatomic) IBOutlet UIWebView *myWebView;

那么,当您已经在 Xib 文件中定义了一个 webView 时,为什么还要创建另一个 UIWebView 并将其分配给 IBOutlet 的 webView?分配一个新的 webView 并将其添加为 subview 是没有意义的。也就是说,您应该丢弃这些行:

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

关于ios - 网站未在 UIWebView 中加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24499328/

相关文章:

ios - 如何在获取通讯录Iphone时知道自己的联系人

ios - 无法通过 TestFlight : Registered maximum number of devices 安装应用

objective-c - 来自 NSString 的精确浮点值

ios - UITableViewCell 交换中的 UILabel 文本值

ios - UIDatePicker 错误?达到最小内部后 UIControlEventValueChanged

带有 url startswith 的 iOS webview

iphone - 在操作表上添加按钮。并且按钮应该超过5个

ios - 为标识符注册的无效 Nib

ios - 在 TableView 的单元格中创建 TableView

ios - 参数标签(_ :)与任何可用的重载都不匹配