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

时间:2011-04-22 08:13:18

标签: iphone objective-c

我有3个单元格的tableView。当用户单击单元格时,它会推送到webViewController。

在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;

我的问题是,如何根据用户点击进入此视图的单元格来更改urlAddress?

就像他们在indexPath.row == 0选择了单元格一样,然后加载google.com,index.row == 1,加载facebook.com等。

1 个答案:

答案 0 :(得分:1)

你在这个方法中做的工作是检测行是否被选中。

- (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...
} 

确保您能够访问urlAddress,并在webviewcontroller.h中合成属性。

ok假设你有一个WebViewController类,在它的头文件中。

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

@property (nonatomic, retain) NSString *urlAddress;

@end

//WebViewController.m
@implementation WebViewController

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