2014-06-30 94 views
0

我已經設置了我的代碼,以便當用戶點擊UITableView中的單元格時,它將繼續到WebViewController,並沿途傳遞該單元格的「Item URL」屬性。在WebViewController類中,我初始化一個UIWebView,並讓它加載相應的URL。出於某種原因,它顯示爲空白,並且不執行任何加載。如何設置WebViewController開始加載網頁?網站不加載在UIWebView

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 

回答

1

這行代碼表明,你在你的XIB文件的WebView和你連接到它的出口:

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

所以,當你已經在你的XIB文件中定義的web視圖,爲什麼你創建另一個UIWebView並將其分配給IBOutlet的webView?分配新的webView並將其添加爲子視圖沒有任何意義。也就是說,你應該拋棄這些行:

_myWebView=[[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)]; 

_myWebView.delegate=self; 

[self.view addSubview:_myWebView]; 

的原因是,當你已經在廈門國際銀行文件的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]; 
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]; 

} 
+0

這幾乎正是我有。 – Ghobs

+0

MatchCenterViewController中的屬性itemURL的類型爲NSURL。你不需要在你的WebviewController中有一個字符串屬性。如果仔細觀察,你會發現不同之處。 – userx

1

有在你的代碼中的錯誤:

NSURL *url = [NSURL URLWithString:_itemURL]; 
NSURLRequest *request = [NSURLRequest requestWithURL:url]; 

_itemURL是一個NSURL對象,而不是NSString。你應該可以做

NSURLRequest *request = [NSURLRequest requestWithURL:_itemURL]; 

提供_itemURL設置。

+0

謝謝,這絕對是一個更好的方法來做到這一點。不幸的是,當我點擊一個單元格時,它仍然無法加載網站。 – Ghobs