简体   繁体   English

UIWebView:根据选择的单元格更改URL?

[英]UIWebView: Change URL Based On What Cell Selected?

I have tableView with 3 cells. 我有3个单元格的tableView。 When user clicks cell, it pushes to a webViewController. 当用户单击单元格时,它将推送到webViewController。

in webViewController's viewDidLoad i have: 在webViewController的viewDidLoad中,我有:

    //A URL STRING
    NSString *urlAddress = @"http://google.com";

    //Create a URL object FROM THAT STRING
    NSURL *url = [NSURL URLWithString:urlAddress];

    //URL Requst Object CREATD FROM YOUR URL OBJECT
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];

    //Load the request in the UIWebView.
    [wView loadRequest:requestObj];

    //scale the page to the device - This can also be done in IB if you prefer
    wView.scalesPageToFit = YES;

My question is, how can I change the urlAddress based on what cell the user clicked to get to this view? 我的问题是,如何根据用户单击以进入此视图的单元格来更改urlAddress?

Like if they selected cell at indexPath.row == 0, then load google.com, index.row == 1, load facebook.com, etc. 就像他们在indexPath.row == 0处选择单元格一样,然后加载google.com,index.row == 1,加载facebook.com等。

you do the job in this method, which is to detect the row is being selected or not. 您可以使用此方法执行该工作,该方法将检测是否选择了该行。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
 //do your webviewcontroller declaration here.
 WebViewController *wvc = [WebViewController alloc] initWithNib ...];

 if(indexPath.row == 0){
   wvc.urlAddress = @"http://google.com";
 }else if(indexPath.row == 1){
   wvc.urlAddress = @"http://facebook.com";
 }else{
   wvc.urlAddress = @"http://abc.com";
 }
//then open that view here...
} 

make sure you able to access the urlAddress, and property and synthesize in webviewcontroller.h. 确保您能够访问urlAddress和属性并在webviewcontroller.h中进行合成。

ok assume you have a WebViewController class, in it header file. 好的,假设您在头文件中有一个WebViewController类。

//WebViewController.h
@interface WebViewController : UIViewController{
    NSString *urlAddress;
}

@property (nonatomic, retain) NSString *urlAddress;

@end

//WebViewController.m
@implementation WebViewController

@synthesize urlAddress;
//... example ...
@end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM