简体   繁体   中英

UIWebView: Change URL Based On What Cell Selected?

I have tableView with 3 cells. When user clicks cell, it pushes to a webViewController.

in webViewController's viewDidLoad i have:

    //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?

Like if they selected cell at indexPath.row == 0, then load google.com, index.row == 1, load facebook.com, etc.

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.

ok assume you have a WebViewController class, in it header file.

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

@property (nonatomic, retain) NSString *urlAddress;

@end

//WebViewController.m
@implementation WebViewController

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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